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. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 |
|
|||
|
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 > > > > |
|
|||
|
"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 |
|
|||
|
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 |
|
|||
|
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 > |
|
|||
|
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 |
|
|||
|
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> |
|
|||
|
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> |
|
|||
|
"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 |
|
|||
|
"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 |