473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Instantiate child object from inside parent constuctor?

Hello,

Is it possible to instantiate a child class within the constructor of its
parent?

eg

Class DBI extends DB {
function DBI() {
// explicit parent constructor call
$this->DB();
$class = get_class($this);
if ($class != '' && $class != 'dbi') { // only do this
for child classes
$obj = new $class; // BORK!!

<snip>

I thought the syntax would be

$obj = new DBI::$class;

but that throws the lovely Hebrew error:

parse error, unexpected T_PAAMAYIM_NEKUDOTAYIM

Any help with this syntax or is this a forbidden act?

Thanks,

jg

Jul 17 '05 #1
2 2466
On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Is it possible to instantiate a child class within the constructor of its
parent?
I suppose it should work; the question is WHY would you want to do
this?!?
Class DBI extends DB {
function DBI() {
// explicit parent constructor call
$this->DB();
$class = get_class($this);
if ($class != '' && $class != 'dbi') { // only do this
for child classes
$obj = new $class; // BORK!!
What do you mean by "BORK!".. ;)

If a class subclasses DBI, and then called it's parent constructor,
this code would result in an endless loop. When the line "new $class"
is executed the constructor is called to instantiate the object and
this constructor is called again and so on until you run out of stack
space.
Any help with this syntax or is this a forbidden act?


The syntax is fine to me but whatever you are trying to accomplish
will not be accomplished by this. What is the purpose of all this?

Later,

Jul 17 '05 #2
Wayne,

Thank you for the reply. I hadn't considered the recursion problem. What I
was trying to do was create automatic accessors based on arrays in my
objects for has_a and has_many relationships. I put the code in the parent
class for ease of access to child classes and...duh...created deep
recursion.

Thanks,

I can see my way out now!

jg
"Wayne" <no*@here.com> wrote in message
news:66********************************@4ax.com...
On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarciuh"
<de*****@no.spam.nolaflash.com> wrote:
Is it possible to instantiate a child class within the constructor of its
parent?


I suppose it should work; the question is WHY would you want to do
this?!?
Class DBI extends DB {
function DBI() {
// explicit parent constructor call
$this->DB();
$class = get_class($this);
if ($class != '' && $class != 'dbi') { // only do this
for child classes
$obj = new $class; // BORK!!


What do you mean by "BORK!".. ;)

If a class subclasses DBI, and then called it's parent constructor,
this code would result in an endless loop. When the line "new $class"
is executed the constructor is called to instantiate the object and
this constructor is called again and so on until you run out of stack
space.
Any help with this syntax or is this a forbidden act?


The syntax is fine to me but whatever you are trying to accomplish
will not be accomplished by this. What is the purpose of all this?

Later,

Jul 17 '05 #3

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

Similar topics

8
4918
by: CJack | last post by:
hy, I have an mdi application, i create a child form and I want to know when a button is pressed while that child form is loaded. I have this code: private void frmTestBaby_KeyUp(object sender,...
1
11537
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...
10
3983
by: Charles Law | last post by:
For some reason, when I click the X to close my MDI parent form, the action appears to be re-directed to one of the MDI child forms, and the parent remains open. I am then unable to close the...
5
3517
by: PAUL | last post by:
Hello, I have 2 tables with a relationship set up in the dataset with vb ..net. I add a new record to the parent table then edit an existing child record to have the new parent ID. However when I...
10
19566
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...
5
4443
by: vul | last post by:
I have an MDI application with a ListBar on the left side of MDI form. All child forms are displayed inside of the client area of MDI form. Now I need to display a third form from a child and...
0
8550
by: Innova | last post by:
Hi, We are working on a gridview inside the gridview (parent-child) scenario. The data of child grid will depend on the data of parent. Objectives: 1.Add new row in parent grid after each row...
6
6066
by: reandeau | last post by:
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;...
3
3474
by: Peted | last post by:
I have an application with mdi parent form, and mutliple child forms. In the mdi parent form i want to instantiate a device class for comunicating to a peice of hardware via serial port. its...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7118
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7493
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...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5049
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...
0
4706
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...
0
1550
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 ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.