473,382 Members | 1,441 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.

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


Jul 17 '05 #1
14 3770
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" <pa****@exeit.removethis.demon.nl> wrote in message
news:10*************@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

Jul 17 '05 #2

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:ca******************@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
Jul 17 '05 #3
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

Jul 17 '05 #4
pablo wrote:
"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:ca******************@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

Jul 17 '05 #5
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" <bd*****************@free.quelquepart.fr> wrote in
message news:40**********************@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

Jul 17 '05 #6
In article <ca******************@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>
Jul 17 '05 #7
In article <10*************@corp.supernews.com>, pablo wrote:

"Tony Marston" <to**@NOSPAM.demon.co.uk> wrote in message
news:ca******************@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>
Jul 17 '05 #8

"Bruno Desthuilliers" <bd*****************@free.quelquepart.fr> wrote in
message news:40**********************@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

Jul 17 '05 #9

"pablo" <pa****@exeit.removethis.demon.nl> wrote in message
news:10*************@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

Jul 17 '05 #10
Tony Marston a écrit :
"Bruno Desthuilliers" <bd*****************@free.quelquepart.fr> wrote in
message news:40**********************@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.

That's not what you said... May I quote ?

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

PHP and Java are far from being "all" OOPls (and I'm not quite sure PHP4
can really be classified as OO)... If I didn't know who you are, I would
have called you a clueless newbie !-)
Bruno
Jul 17 '05 #11
pablo a écrit :
(please dont top-post -- fixed)

"Bruno Desthuilliers" <bd*****************@free.quelquepart.fr> wrote in
message news:40**********************@news.free.fr...
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...)
(snip code)
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 :
(snip more code)
In my view personnel inherits some properties of the class humans, f.i.
name, sex, date of birth.
yes, but this does not make a 'personnel' a subclass of human. Let's
have one simple exemple : in a business app, I have entities[1] like
customers, employee and suppliers. I also have entities like individuals
and companies.

[1] "entities", not "classes". It's analysis, not design nor
implementation, right ?

Now here are (some of) the rules :
an employee is an individual
a customer may be either an individual or a company
a supplier may be either an individual or a company
an employee may also be a customer
a supplier may also be a customer
a customer may also be a customer
an individual, formerly employee, may stop being an employee and become
a supplier

As you can see, we can not model this with a simple "class and
inheritence" scheme. So let's analyse further and find what is *not*
subject to change. This bring us to :
- a company is always a company
- an individual is always an individual

The other rules can now be expressed as :
- both can act as suppliers and/or as customers
- only individuals can act as employees

Clearly, we have to notions : "being" (individual, company) and "acting
as" (a customer, supplier, employee). The first notion wont change with
time, the second will.

From a *design* POV, modeling 'employee' as a subclass of individual
would be an error, since an 'individual' object should 'jump' from one
class to another when it becomes an employee or supplier or stop being one.

The best design I could think of in this exemple is to have classes
"Company" and "Individual", and <role> classes "Customer", "Supplier"
and "Employee". Customer and Supplier instances are 'composed of' either
a Company or Individual instance, to which they delegate some
responsabilities. Employee instances are 'composed of' an Individual
instance, to which etc...
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.
You can also do this with [aggregation or composition] and delegation.
Now, humans have to exist before they can become personnel.
This only depends on the business rules of *your* application. If you're
only interested on employees, you don't even need the 'individual' class.
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?
You don't "add" an object "to" a class. An object is an *instance* of
one particular class (and - if you respect the substitution principle -
of its parent classes).

What you ask for is "how do I build a 'personnel' object from a 'human'
object. This answer is quite evident : pass the a 'human' instance as a
parameter to the 'personnel' constructor. Once it's done, you have to
ways to proceed :

- the dirtyHackSolution : copy attributes of the 'human' object to
attributes of the 'employee' object. This seems quite simple and
obvious, but also leads to an obvious wart : once the copy is done, you
have *two different objects*.

- the cleanDesignSolution : just store that reference in the 'personnel'
instance, and delegate every 'human' specific message to it (you can
even add some 'personnel'-specific behavior before and/or after the
'delegate' call).

Now none of those two solutions is an 'absolute best choice', this all
depends on the problem your app needs to solve (the 'business domain' if
you want to be buzzword-compliant !-) and what language you're using
(ok, here it's PHP, land of all tricks... ).

The dirtyHackSolution may be a 'good enough' solution, as long as 1/
you're confident in the fact that no one will talk to the 'human'
instance once it has been copied to a 'personnel', and 2/requirements
dont change.
That is all I need and want.
That may be all you want, but not what you really need !-)
(but this, only *you* - and your app's users - can tell).
Tia

HTH
Bruno
Jul 17 '05 #12

"bruno modulix" <on***@xiludom.gro> wrote in message
news:40***********************@news.free.fr...
Tony Marston a écrit :
"Bruno Desthuilliers" <bd*****************@free.quelquepart.fr> wrote in
message news:40**********************@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.

That's not what you said... May I quote ?

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


What I actually wrote was:-
<quote> 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.</quote>

The statement:
<quote>Each child class can have only one parent.</quote>
applies to PHP in particular as it does not support multiple inheritance
(just like Java).
PHP and Java are far from being "all" OOPls (and I'm not quite sure PHP4
can really be classified as OO).
PHP 4 does support Object Oriented programming in that you can achieve
encapsulation, inheritance and polymorphism which are the fundamental
principles of OO. The fact that it does not support ffeatures that other
people dreamed up later is (IMHO) totally irrelevant.
If I didn't know who you are, I would
have called you a clueless newbie !-)


I am used to people calling me names when I express opinions which are
different from theirs.

--
Tony Marston

http://www.tonymarston.net

Jul 17 '05 #13

"pablo" <pa****@exeit.removethis.demon.nl> wrote in message
news:10*************@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


Well, pablo wrote .....
and got several interesting replies.

I sincerely thank those that enlightened me.

Thank you!

pablo

Jul 17 '05 #14
Tony Marston wrote:
"bruno modulix" <on***@xiludom.gro> wrote in message
news:40***********************@news.free.fr...
(snip some precisions...)
PHP and Java are far from being "all" OOPls (and I'm not quite sure PHP4
can really be classified as OO).

PHP 4 does support Object Oriented programming in that you can achieve
encapsulation, inheritance and polymorphism which are the fundamental
principles of OO. The fact that it does not support ffeatures that other
people dreamed up later is (IMHO) totally irrelevant.


I forgot to enclose my later remark in a pair of '<troll>' tags, sorry !-)

for ($i = 0; $i < 100; $i++)
echo "I shall not forget the '&lt;troll&gt;' tags no more<br>";
If I didn't know who you are, I would
have called you a clueless newbie !-)


I am used to people calling me names when I express opinions which are
different from theirs.


Tony, you understand what a smiley is, don't you ?-)
Jul 17 '05 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Raj | last post by:
Hi, I am trying to add some more information to the table which already has a lot a data (like 2-3000 records). The new information may be adding 2-3 new columns worth. Now my questions are:...
1
by: Earl Teigrob | last post by:
I did a ton of searching to try and find a simple solution to this issue and finally wrote my own, which I am sharing with everyone. In my searching, I did find a very complete and robust solution at...
20
by: Bryan | last post by:
hello all... im trying to add a record to an sql db on ms sql server 2000, using vb.net. seems to be working.. except for one thing, one of the columns in the database is a bit datatype, and...
16
by: Geoff Jones | last post by:
Hi Can anybody help me with the following, hopefully simple, question? I have a table which I've connected to a dataset. I wish to add a new column to the beginning of the table and to fill...
10
by: Goran Djuranovic | last post by:
Hi all, Does anyone know how to declare a variable in a class to be accessible ONLY from a classes instantiated within that class? For example: ************* CODE ***************** Public...
0
by: ILCSP | last post by:
Hello, I have a VB.Net project where I have created a dynamic menu. This menu has 2 parents (File and Edit) and I need to place 3 child submenus under Edit at runtime. The problem is that I am...
7
by: Ron | last post by:
Hi All, Using Access2000, winXP. Table 1 = tblClients displayed on frmClients via qryClients. 2nd table = tblInvoices shown on frmInvoices via qryInvoices. 2nd table = tblDetails shown on...
1
by: Rajarshi | last post by:
Hi, I'm using ElementTree for some RSS processing. The point where I face a problem is that within an <item></itemI need to add another child node (in addition to <linketc) which is a well-formed...
6
by: SQACSharp | last post by:
I'm using the EnumChildWindows API with an EnumChildWndProc callback to populate the treeview. The output will be something similar to spy+ + How can I specify the parent when adding a new node...
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: 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.