By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,885 Members | 1,469 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,885 IT Pros & Developers. It's quick & easy.

Instantiate child object from inside parent constuctor?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.