473,545 Members | 1,773 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_NEKU DOTAYIM

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

Thanks,

jg

Jul 17 '05 #1
2 2472
On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarci uh"
<de*****@no.spa m.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...cre ated deep
recursion.

Thanks,

I can see my way out now!

jg
"Wayne" <no*@here.com > wrote in message
news:66******** *************** *********@4ax.c om...
On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarci uh"
<de*****@no.spa m.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
4921
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, System.EventArgs e) { MessageBox.Show("keyboard button pressed!"); } Following is the code to load the frmTestBaby
1
11545
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...
10
3989
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 application. What should happen, is that the main MDI form should close, taking the child forms with it. There is code to loop through the child forms,...
5
3523
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 do the update the changed parentid in the child table fails to change. No error is given its just that the change is not written to the Database. ...
10
19582
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"
5
4447
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 center it related to the calling Child form. I cannot find the way to do that. Any help please Al
0
8555
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 and add child grid into that row because the columns in child grid are same as parent grid and we want to align them with the cols of parent grid....
6
6069
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; protected $user; protected $output; public function __construct($database,$user,$output) { $this->database = $database;
3
3476
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 constructor would look something like Device kvm = new Device(Serialport);
0
7391
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7802
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...
1
7410
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5320
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...
0
4941
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...
0
3443
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...
0
3438
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1869
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
1
1010
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.