By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,127 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,127 IT Pros & Developers. It's quick & easy.

Child construct parent

P: n/a
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?
Thanks

Jon

Aug 16 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On 16 Aug 2006 14:38:57 -0700, "reandeau" <jo***********@gmail.comwrote:
>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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Aug 16 '06 #2

P: n/a
....
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 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
child.

Best regards
Aug 16 '06 #3

P: n/a

Andy Hassall wrote:
On 16 Aug 2006 14:38:57 -0700, "reandeau" <jo***********@gmail.comwrote:
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 :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Thanks for the help. That makes sense.

Aug 16 '06 #4

P: n/a
reandeau 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?
Thanks

Jon
Jim,

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

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.
js*******@attglobal.net
==================
Aug 17 '06 #5

P: n/a
I think you're confusing programming inheritance with runtime
inheritance. Two different child objects will have two different parent
objects.

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?
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
child.

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.

Jon

Aug 17 '06 #6

P: n/a

Dikkie Dik wrote:
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.
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)

See http://us2.php.net/manual/en/language.exceptions.php

Aug 18 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.