adding existing parent object to extended child object?

This is a discussion on adding existing parent object to extended child object? within the PHP Language forums, part of the PHP Programming Forums category; Dear NewsGroupers, I am relatively new to OOP and cannet get my head around this problem. I have two classes. ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 06-09-2004
pablo
 
Posts: n/a
Default adding existing parent object to extended child object?

Dear NewsGroupers,

I am relatively new to OOP and cannet get my head around this problem.
I have two classes. Class Child extends Parent.
There is no constructor for the Child class. So when I create a child object
the constructor for the parent object is called. That works fine.

But now I have the problem that I want to add an already existing Parent
object to create a new Child object. How can this be done?

TIA

pablo




Reply With Quote
  #2 (permalink)  
Old 06-09-2004
Tony Marston
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

Once you have extended a (parent) class and created a (child) subclass it is
simply not possible to add another parent to that child class. This applies
to all OO languages, not just PHP. Each child class can have only one
parent.

What is it exactly that you are trying to do?

--
Tony Marston

http://www.tonymarston.net



"pablo" <pablok@exeit.removethis.demon.nl> wrote in message
news:10cdjsvte5l549f@corp.supernews.com...
> Dear NewsGroupers,
>
> I am relatively new to OOP and cannet get my head around this problem.
> I have two classes. Class Child extends Parent.
> There is no constructor for the Child class. So when I create a child

object
> the constructor for the parent object is called. That works fine.
>
> But now I have the problem that I want to add an already existing Parent
> object to create a new Child object. How can this be done?
>
> TIA
>
> pablo
>
>
>
>



Reply With Quote
  #3 (permalink)  
Old 06-09-2004
pablo
 
Posts: n/a
Default Re: adding existing parent object to extended child object?


"Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
news:ca6odi$mg$1$8302bc10@news.demon.co.uk...
> Once you have extended a (parent) class and created a (child) subclass it

is
> simply not possible to add another parent to that child class. This

applies
> to all OO languages, not just PHP. Each child class can have only one
> parent.


The parent class is the same.
Suppose the parent class is humans and the child class is personnel.
I already have created some humans and want to add these to the personnel

pablo


Reply With Quote
  #4 (permalink)  
Old 06-09-2004
Bruno Desthuilliers
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

Tony Marston wrote:
> Once you have extended a (parent) class and created a (child) subclass it is
> simply not possible to add another parent to that child class. This applies
> to all OO languages, not just PHP. Each child class can have only one
> parent.
>


Tony, I hope this is not what you meant ? Many (non cryptic) OOPLs offer
multiple inheritence (C++, Python, Common Lisp...).

<op>
Now it's true that PHP only support single inheritence. But inheritence
is far from being the alpha and omega of OOP. Aggregation|composition +
delegation is another way to reuse code (ok, this means having more code
to write, but what, this is straightforward...)

ex :

class Parent
{
function Parent($args) { ... }
function dothis($args) { ... }
}

class Mixin
{
function dothat($withit) { ... }
}

class Child extends Parent
{
function Child($args)
{
parent::Parent($args);
[...]
$this->mixin =& new Mixin();
}

function dothat($withit)
{
return $this->mixin->dothat($withit);
}
}

Now you can use Child as either a Parent object, a Child object, or a
Mixin object...

One advantage of aggregation|composition ws inheritence is that it can
be dynamic :

class Mixin2
{
function dothat($withit) { [not the same code as Mixin's] }
}

class Child2 extends Parent
{
function Child($args, $mixinKlassname)
{
parent::Parent($args);
[...]
$this->mixin =& new $mixinKlassname();
}

function dothat($withit)
{
return $this->mixin->dothat($withit);
}
}

c2one =& new Child2($args, 'Mixin');
c2two =& new Child2($args, 'Mixin2');

HTH
Bruno

Reply With Quote
  #5 (permalink)  
Old 06-09-2004
pablo
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

In my view personnel inherits some properties of the class humans, f.i.
name, sex, date of birth.

When I extend the class personnel with humans I can use the properties and
methods of humans and add to these some other properties and methods.
Now, humans have to exist before they can become personnel. So when I
instantiate personnel the humans constructor is automatically called. No
Worries.
But how can I add a human(s) that already exist to my personnel class?
That is all I need and want.

Tia

pablo

"Bruno Desthuilliers" <bdesth.quelquechose@free.quelquepart.fr> wrote in
message news:40c6f920$0$7708$636a15ce@news.free.fr...
> Tony Marston wrote:
> > Once you have extended a (parent) class and created a (child) subclass

it is
> > simply not possible to add another parent to that child class. This

applies
> > to all OO languages, not just PHP. Each child class can have only one
> > parent.
> >

>
> Tony, I hope this is not what you meant ? Many (non cryptic) OOPLs offer
> multiple inheritence (C++, Python, Common Lisp...).
>
> <op>
> Now it's true that PHP only support single inheritence. But inheritence
> is far from being the alpha and omega of OOP. Aggregation|composition +
> delegation is another way to reuse code (ok, this means having more code
> to write, but what, this is straightforward...)
>
> ex :
>
> class Parent
> {
> function Parent($args) { ... }
> function dothis($args) { ... }
> }
>
> class Mixin
> {
> function dothat($withit) { ... }
> }
>
> class Child extends Parent
> {
> function Child($args)
> {
> parent::Parent($args);
> [...]
> $this->mixin =& new Mixin();
> }
>
> function dothat($withit)
> {
> return $this->mixin->dothat($withit);
> }
> }
>
> Now you can use Child as either a Parent object, a Child object, or a
> Mixin object...
>
> One advantage of aggregation|composition ws inheritence is that it can
> be dynamic :
>
> class Mixin2
> {
> function dothat($withit) { [not the same code as Mixin's] }
> }
>
> class Child2 extends Parent
> {
> function Child($args, $mixinKlassname)
> {
> parent::Parent($args);
> [...]
> $this->mixin =& new $mixinKlassname();
> }
>
> function dothat($withit)
> {
> return $this->mixin->dothat($withit);
> }
> }
>
> c2one =& new Child2($args, 'Mixin');
> c2two =& new Child2($args, 'Mixin2');
>
> HTH
> Bruno
>



Reply With Quote
  #6 (permalink)  
Old 06-09-2004
Bruno Desthuilliers
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

pablo wrote:
> "Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
> news:ca6odi$mg$1$8302bc10@news.demon.co.uk...
>
>>Once you have extended a (parent) class and created a (child) subclass it

>
> is
>
>>simply not possible to add another parent to that child class. This

>
> applies
>
>>to all OO languages, not just PHP. Each child class can have only one
>>parent.

>
>
> The parent class is the same.
> Suppose the parent class is humans and the child class is personnel.
> I already have created some humans and want to add these to the personnel


Err... I guess that you're confused about what is inheritence and what
it's good for. Conceptually, 'human' is a class, but 'personnal' is not
a subclass of 'human', it's a role that a human can play. Here you'd
better go with composition :

class Human
{
function Human($whatever) { ... }
....
}

class Personnal
{
var $human;

function personnal($human, $otherargs)
{
$this->human = $human;
}

...
}

$p =&new Personnal(new Human($whatever));

or

$h =& new Human($whatever);
$p =& new Personnal($h);

HTH
Bruno

Reply With Quote
  #7 (permalink)  
Old 06-09-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

In article <ca6odi$mg$1$8302bc10@news.demon.co.uk>, Tony Marston wrote:
> Once you have extended a (parent) class and created a (child) subclass it is
> simply not possible to add another parent to that child class. This applies
> to all OO languages, not just PHP. Each child class can have only one
> parent.


C++ allows multiple inheritance.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Reply With Quote
  #8 (permalink)  
Old 06-09-2004
Tim Van Wassenhove
 
Posts: n/a
Default Re: adding existing parent object to extended child object?

In article <10cds9bm3l4t109@corp.supernews.com>, pablo wrote:
>
> "Tony Marston" <tony@NOSPAM.demon.co.uk> wrote in message
> news:ca6odi$mg$1$8302bc10@news.demon.co.uk...
>> Once you have extended a (parent) class and created a (child) subclass it

> is
>> simply not possible to add another parent to that child class. This

> applies
>> to all OO languages, not just PHP. Each child class can have only one
>> parent.

>
> The parent class is the same.
> Suppose the parent class is humans and the child class is personnel.
> I already have created some humans and want to add these to the personnel


You can try to downcast the human objects to personnel objects.

--
Tim Van Wassenhove <http://home.mysth.be/~timvw/contact.php>
Reply With Quote
  #9 (permalink)  
Old 06-09-2004
Tony Marston
 
Posts: n/a
Default Re: adding existing parent object to extended child object?


"Bruno Desthuilliers" <bdesth.quelquechose@free.quelquepart.fr> wrote in
message news:40c6f920$0$7708$636a15ce@news.free.fr...
> Tony Marston wrote:
> > Once you have extended a (parent) class and created a (child) subclass

it is
> > simply not possible to add another parent to that child class. This

applies
> > to all OO languages, not just PHP. Each child class can have only one
> > parent.
> >

>
> Tony, I hope this is not what you meant ? Many (non cryptic) OOPLs offer
> multiple inheritence (C++, Python, Common Lisp...).


I meant that PHP does not support multiple inheritance. Neither does Java.

--
Tony Marston

http://www.tonymarston.net



Reply With Quote
  #10 (permalink)  
Old 06-09-2004
Tony Marston
 
Posts: n/a
Default Re: adding existing parent object to extended child object?


"pablo" <pablok@exeit.removethis.demon.nl> wrote in message
news:10cdvui4lo5ag04@corp.supernews.com...
> In my view personnel inherits some properties of the class humans, f.i.
> name, sex, date of birth.
>
> When I extend the class personnel with humans I can use the properties and
> methods of humans and add to these some other properties and methods.
> Now, humans have to exist before they can become personnel. So when I
> instantiate personnel the humans constructor is automatically called. No
> Worries.
> But how can I add a human(s) that already exist to my personnel class?
> That is all I need and want.


Let me see if I understand this....

You have a HUMAN class.
You have a PERSONNEL class which is a subclass of HUMAN.
You have created an instance of HUMAN but now want to make it an instance of
PERSONNEL.

There must be some difference between the properties of HUMAN and PERSONNEL,
so surely all you need to do is transfer the properties of your HUMAN
instance to a PERSONNEL instance.

--
Tony Marston

http://www.tonymarston.net



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:45 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0