473,382 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Child construct parent

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
6 6054
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
....
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

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
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
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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ahaideb | last post by:
I have a table (relation) in my database: --------------- | parent | child | --------------- | 1 | 2 | | 1 | 3 | | 2 | 4 | | 2 | 5 ...
4
by: Danny Tuppeny | last post by:
Hi all, I've been trying to write some classes, so when I have a parent-child relationship, such as with Folders in my application, I don't have to remember to add a parent reference, as well as...
10
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
7
by: msxkim | last post by:
How to execute functions in the parent class first and then functions in the child class? For example, I have a parent class with functions 'ONE' and 'TWO' and child class has a function 'THREE'. ...
5
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
8
by: Dip | last post by:
Hello Experts, Here is the code to flatten a PC hierarchy into a level based table. It works fine. SELECT t1.TASK_ID AS TASK_LV1, t2.TASK_ID AS TASK_LV2, t3.TASK_ID AS TASK_LV3, t4.TASK_ID AS...
0
by: Ahmed Yasser | last post by:
Hi everybody, I have just joined the forum hoping someone would help. Shortly, my problem is regarding the Datagrid control in the windows VB application . Net 2003. I have managed to construct a...
1
by: =?Utf-8?B?cmFuZHkxMjAw?= | last post by:
The code below is pretty simple. Calling Talker() in the parent returns "Parent", and calling Talker() in the child returns "Child". I'm wondering how I can modify the code so that a call to the...
4
by: jewel87 | last post by:
Hi everyone! I'm writing some code in C under UNIX, which should give some output like this: PARENT: pid = 10063 CHILD: my pid = 10064 CHILD: my parent's pid = 10063 CHILD: Sleeping... PARENT:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.