Re: Can a class instance a property of another class
| From: | Jochem Maas | Date: | Fri, 26 Jan 2007 23:47:11 +0000 |
| Subject: | Re: Can a class instance a property of another class | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-247865@lists.php.net to get a copy of this message | ||
Ken Kixmoeller -- reply to ken@kixmoeller.com wrote:
> Hey - -- -
>
> Here I am again. Anybody still working on a Friday?
>
> I would like to have a class instance be the property of another class,
> like can be done in other languages. For example: I would like to have a
> "Connections" class which contains all of the database connection logic
> and query results. There are advantages to having this type of utility
> class be local to a data or business class. (I know that I could have a
> generic "include" with functions outside of the class hierarchy.)
>
> So, in the __construct method of a business or data class, for example,
> one could:
>
> include_once("connection_classes.kbk");
> $this->connection_class = new connection_class;
>
> This syntax fails, so I know this isn't right, but I hope you get the idea.
what fails? I can't smell the error your getting from here and from what I see
there should be no problem:
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"; }
}
$b = new Bar;
$b->speak();
>
> Can it be done?
yes. see above. :-)
>
> TIA, again
>
> Ken
>
> --PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>