You are here: Re: Child construct parent « PHP Programming Language « IT news, forums, messages
Re: Child construct parent

Posted by Jerry Stuckle on 08/17/06 12:19

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.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация