Reference to THIS from inside constructor

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

    #1

    Reference to THIS from inside constructor

    Hi there,
    I heard storing reference to THIS from inside object's constructor can
    cause problems:

    $myObjects = array();
    class MyClass{
    function MyClass(){
    global $myObjects;
    $myObjects[] = $this;
    }
    }

    Do you know anything about that? It looks like when constructor
    finishes all such references lead to COPY of the object.

  • Tom

    #2
    Re: Reference to THIS from inside constructor

    On Feb 1, 9:55 am, "Tamagafk" <tamag...@gmail .comwrote:
    Hi there,
    I heard storing reference to THIS from inside object's constructor can
    cause problems:
    >
    $myObjects = array();
    class MyClass{
    function MyClass(){
    global $myObjects;
    $myObjects[] = $this;
    }
    >
    }
    >
    Do you know anything about that? It looks like when constructor
    finishes all such references lead to COPY of the object.
    If you actually want to reference instead of copy, you need to do:
    $myObjects[] = &$this ;

    You can also do this outside the constructor when defining your
    instance, e.g.:
    $myObjects[] = &$ref ;
    $ref = new MyClass() ;

    Comment

    Working...