473,763 Members | 7,611 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3826
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.r emovethis.demon .nl> wrote in message
news:10******** *****@corp.supe rnews.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.de mon.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|com position +
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|com position 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.de mon.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($huma n, $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.quel quepart.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|com position +
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|com position 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.dem on.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.supernew s.com>, pablo wrote:

"Tony Marston" <to**@NOSPAM.de mon.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.quel quepart.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.r emovethis.demon .nl> wrote in message
news:10******** *****@corp.supe rnews.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

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

Similar topics

3
4909
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)Is it a good idea to add new columns to the existing table? then it will create these new columns for all old records, will it not result in wasting a lot of space?? (2)Is it a good idea to create a new table with the new information and have as a...
1
11581
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 http://weblogs.asp.net/asmith/archive/2003/09/15/27684.aspx but it was far more complex then I needed. (I got lost trying to figure it all out). Therefore, here goes my simple "web dialog box with parent event handler fireing" solution. ...
20
2146
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 though i get no syntax errors when compiling, i get an error indicated that the data would be truncated. the field is login_status. ive tried in quotes and not, giving it an integer variable with the number 1
16
2487
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 it with incremental values e.g. if the tables looks like this: 23 56
10
19624
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 Class Parent '*** HOW TO DECLARE IT *** Dim Age As String = "1/1/2000"
0
1452
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 using dynamic menus where I get the values from a SQL 2000 table. I have a variable called "mnu" as the name of the child menu. I have another variable with the name of the parent menu "sct". I loop through a data table to get the child and...
7
2012
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 subform(to frmInvoices) sfrmDetails via qryDetails. Relationship built between tblClients/tblInvoices/tblDetails by ClientID. Relationship between tblInvoices/tblDetails by InvoiceID. All works fine if
1
2606
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 XML document (Chemical Markup Language to be precise). So my code looks like: import cElementTree as ET c = open('x.cml').readlines()
6
2516
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 ?? When adding a new node is there any way to get an handle or something else to be able add the childs to the correct parent ? Thanks!
0
10148
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10002
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9823
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7368
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6643
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5270
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5406
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3528
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.