[UPHPU] Accessing a Parent Object's Other Variables
Justin Carmony
justin at justincarmony.com
Tue Jun 23 14:17:46 MDT 2009
Probably the easiest example to show for HAS-A relationship instead of
the inheritance "IS-A" is if we just change a few of the variable names
in the original example.
<?php
Class Person {
var $name = 'Justin';
var $pet;
function __construct() {
$this->pet = new Dog($this);
}
function PlayWithPet()
{
echo "Lets play ".$this->pet->name."\n\r";
}
}
Class Dog {
var $name = 'Fido';
var $owner;
function __construct($owner) {
$this->owner = $owner;
}
function LickOwner()
{
echo "*".$this->name." licks ".$this->owner->name."'s face!*";
}
}
$person = new Person();
$pet = $person->pet;
echo $person->name." has a pet named ".$pet->name."\n\r";
$person->PlayWithPet();
$pet->LickOwner();
/* OUTPUT:
Justin has a pet named Fido
Lets play Fido
*Fido licks Justin's face!*
*/
?>
A dog isn't a person, and a person isn't a dog. Yet their owner/pet relationship is important and its good to have a reference between the two.
Justin Carmony
Richard K Miller wrote:
> Here's an article on it, from one of the Gang of Four:
>
> http://www.artima.com/lejava/articles/designprinciples4.html
>
> Richard
>
>
>
>
>
>
>
> On Jun 23, 2009, at 12:13 PM, Kirk Cerny wrote:
>
>> I would still not write it this way, but good point Alvaro.
>>
>> Kirk Cerny
>>
>
>
> _______________________________________________
>
> UPHPU mailing list
> UPHPU at uphpu.org
> http://uphpu.org/mailman/listinfo/uphpu
> IRC: #uphpu on irc.freenode.net
More information about the UPHPU
mailing list