Re: Can a class instance a property of another class

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Edward Kay 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.
> This fails simply because you've omitted the parentheses on the
> constructor. It should be:
> 
> $this->connection_class = new connection_class();

WRONG - the parentheses are optional - my example in a a previous reply
to this thread proves this (if you care to test it).

> 
> Of course, you may want/need to pass some arguments here too.
> 
> As mentioned by others though, for the scenario you describe above, you
> would probably be better extending a base class.

I see no real justification for class extension at this stage, it *may* be the best
way to tackle this situation BUT there may be good reasons that the class in question
lives in it's own seperate class heirarchy.

object aggregation is just as valid as class extending in principle.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux