Re: Can a class instance a property of another class

From: Date: Sat, 27 Jan 2007 22:24:21 +0000
Subject: Re: Can a class instance a property of another class
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-247899@lists.php.net to get a copy of this message
Ken Kixmoeller -- reply to ken@kixmoeller.com wrote:
OK, Jochem, I adapted your example and got it working. Thank you very much. I am still playing with it to better understand. One thing I don't yet understand is the necessity for the getFoo()/getBar() "handshake," especially the getbar() in the BAR class. That doesn't seem to serve any purpose. My adaptation us just a getDummy(). Do they just serve to pass the object by reference? Ken -------------------------------------------------------------- On Jan 26, 2007, at 5:47 PM, Jochem Maas wrote:
class Foo {
    private $var;
    function __construct() { $this->var = "foo"; }
    function getFoo() { return $this->var; }
} class Bar {
    private $var;
    private $foo;
    function __construct() { $this->var = "bar"; $this->foo = new Foo; }
    function getBar() { return $this->var; }
    function speak() { echo "I am ",$this->foo->getFoo(),$this->getBar(),"\n"; }
}
--PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Assigning another class to a class variable is a standard OO technique, so it's not surprising that it's implemented in PHP. I believe it's called "composition", though that term seems to have a wider definition. What is it that seems odd about what you call the getFoo()/getBar() "handshake" ? Actually, what do you mean by that? I could be missing something, but at the moment the two classes and their calls seem pretty straight-forward. -- _____________________ Myron Turner http://www.room535.org http://www.bstatzero.org http://www.mturner.org/XML_PullParser/

« previous php.general (#247899) next »