|
Posted by Andy Hassall on 08/16/06 21:54
On 16 Aug 2006 14:38:57 -0700, "reandeau" <jon.tjemsland@gmail.com> wrote:
>I'm building out a OO based app in PHP 5 but I'm getting a little
>confused on children contructing parents.
>
>I have a parent that looks like this:
>abstract Class State {
> protected $database;
> protected $user;
> protected $output;
> public function __construct($database,$user,$output) {
> $this->database = $database;
> $this->user = $user;
> $this->output = $output;
> }
>}
>
>And a child that looks like this:
>Class Status extends State {
> public function __construct($database,$user,$output) {
> parent::__construct($database,$user,$output);
> }
>}
>
>This seems to work OK. But I'm getting confused when thinking about
>adding a new child who then has to contruct the parent again. Isn't
>this getting away from the whole purpose of inheritence? It seems like
>this is recreating the parent with every new child that come along.
>Would it be possible to just have a single instance of the parent that
>all children extended or am I missing the point here?
You are possibly confusing "is-a" relationships between base class and
subclass with a "has-a" relationship.
Since Status "is a" State, then you need to do all the construction for the
base class attributes first, then the construction for the subclass.
There is no parent instance here - when you create a Status object, there is
only one object; a Status object, which inherits everything that a State object
has and can do, which then adds or overrides data and methods with data and
methods deinfed in the State class.
Now, if there is a common resource you want shared amongst all instances of
the class, then that would be a "has-a" relationship - a variable in each class
instance that is a reference to one separate instance of another object. A
database connection might fall into this sort of category.
--
Andy Hassall :: andy@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
[Back to original message]
|