Instantiate child object from inside parent constuctor?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jerrygarciuh

    #1

    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



  • Wayne

    #2
    Re: Instantiate child object from inside parent constuctor?

    On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarci uh"
    <designs@no.spa m.nolaflash.com > wrote:
    [color=blue]
    >Is it possible to instantiate a child class within the constructor of its
    >parent?[/color]

    I suppose it should work; the question is WHY would you want to do
    this?!?
    [color=blue]
    >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!![/color]

    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.
    [color=blue]
    >Any help with this syntax or is this a forbidden act?[/color]

    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,

    Comment

    • jerrygarciuh

      #3
      Re: Instantiate child object from inside parent constuctor?

      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" <not@here.com > wrote in message
      news:66vp11pm4l s78mr1kd1ucad7k l2d989h2s@4ax.c om...[color=blue]
      > On Wed, 23 Feb 2005 14:33:39 -0600, "jerrygarci uh"
      > <designs@no.spa m.nolaflash.com > wrote:
      >[color=green]
      >>Is it possible to instantiate a child class within the constructor of its
      >>parent?[/color]
      >
      > I suppose it should work; the question is WHY would you want to do
      > this?!?
      >[color=green]
      >>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!![/color]
      >
      > 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.
      >[color=green]
      >>Any help with this syntax or is this a forbidden act?[/color]
      >
      > 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,
      >[/color]


      Comment

      Working...