Child construct parent

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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) {

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?



Re: Child construct parent

Quoted text here. Click to load it

 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 :: :: :: disk and FTP usage analysis tool

Re: Child construct parent

Andy Hassall wrote:
Quoted text here. Click to load it

Thanks for the help.  That makes sense.

Re: Child construct parent

Quoted text here. Click to load it

You are not constructing the parent as a separate object. There is only  
one object that is specifically the child and more generally the parent  
(because the child is a specialization of the parent). However, the  
parent may need some data to be constructed with, even if it is  
abstract. Calling the parent's constructor from the child does just  
that: initialize the code in the parent class. Mind you, due to  
inheritance, that code is now active in the child object.

An example:

class Exception
{private internalmessage;
  public function __construct(message)
          {this->internalmessage = message;}
  public function getMessage()
          {return this->internalmessage;}

class LoginException extends Exception
{public function __construct(message)
          {parent::__construct('Could not verify credentials.');}

An exception always has to have a message, so it enforces one by  
requiring it in the constructor's parameters. This message is kept  
safely inside, so it is effectively read-only. Not even a subclass can  
touch it.
The subclass LoginException sets an appropriate message by calling its  
parent's constructor (that is not done automatically by PHP). But as the  
internalmessage in the superclass is private, it is not within reach of  
the subclass. Off course, an instance of LoginException will have this  
variable somewhere in memory, but only the methods defined in the  
superclass can touch it. So there is just one instance then: that of the  

Best regards

Re: Child construct parent

Dikkie Dik wrote:
Quoted text here. Click to load it

This is incorrect:
* The properties of the PHP5 Exception class are all protected.  They
can be directly accessed by derived classes, but not outside the class.
* The constructor of a PHP5 Exception defaults to (null, 0), so niether
argument is required.
* It should be noted that the methods of the exception class cannot be
overridden, they are declared 'final,' (except __toString)


Re: Child construct parent

reandeau wrote:
Quoted text here. Click to load it


I think you're confusing programming inheritance with runtime  
inheritance.  Two different child objects will have two different parent  

For instance - let's say you have the parent class 'Person'.  As  
children of that class you have 'Employee' and 'Consultant'.  Child  
classes of 'Employee' are 'HourlyEmployee' and 'SalariedEmployee'.

They could have attributes such as:

  Person: Name, Address, etc.
  Employee: Employee ID, Department
  HourlyEmployee: Hourly rate
  SalariedEmployee: Weekly salary
  Consultant: Consulting rate

Now - you do:

   $tom = new HourlyEmployee("Tom Jones", ...);
   $dick = new SalariedEmployee("Dick Anderson", ...);
   $harry = new Consultant("Harry Smith", ...);

You would need three separate Person objects.  You wouldn't want just  
one Person object - you wouldn't be able to keep all three names in it.

Does this help?

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Re: Child construct parent

Quoted text here. Click to load it

Yea that helps tremendously.  I was getting caught up with my runtime
program needing to look like my diagram.  There was only one parent in
my diagram, so I was thinking "why should there be multiple parent
objects during runtime".  I can see from what you stated above that
there is a need to have a parent object for each child to keep the
values stored in inherited variables associated with their prospecitve

I think a big part of my issue here was that old procedural programmer
coming out in me and wanting to make the code as efficient (read: as
short) as possible.  Creating all of these parent objects just didn't
seem efficient and I had gotten caught up in the idea that inheritence
must be purely for efficiency.  But I can see now that, like most
things OO, inheritence is there to help make your objects as resusable
as possible- especially if done correctly.

Thanks so much for your help.


Site Timeline