ID: 9771
Updated by: stas
Reported By: [EMAIL PROTECTED]
Old-Status: Open
Status: Closed
Bug Type: Class/Object related
Assigned To: 
Comments:

You should use:

    $oTest0 =& new Test0_t();

(new PHP 4.0.4 syntax) to do what you want. Generally, if
you reference $this from the constructor and store it, you
should use this syntax, otherwise the result of the new is
not the same object as $this in the constructor.

Previous Comments:
---------------------------------------------------------------------------

[2001-03-15 14:48:54] [EMAIL PROTECTED]
Hello,

The example that follows this little description should, as far as my understanding of 
PHP goes, demonstrate proper passing of multilayered objects, by reference, to 
independent sub-objects.  The more understandable description, there exists a 
super-class that has two sub-classes.  The first sub-class, upon construction, is 
linked to the second sub-class (via a parameter passed to the first class' 
constructor).  The first sub-class' constructor sets a variable, local to that first 
sub-class, to be a reference to the second sub-class.  The second sub-class is 
comprised only of a variable that will be changed by functionality defined in the 
first sub-class.

The first code snippet does not function properly.  It seems that the first sub-class 
does not truly create a reference, but creates a copy of the second sub-class (as it 
exists upon construction of the first sub-class).  Therefore, it can not be changed, 
only read.

The second code snippet functions properly and implements what I label a hack around.  
This code snippet accesses the second sub-class within the constructor of the first 
sub-class.  In other words, it seems that PHP does not create a reference, unless 
something within that reference is accessed within the function defining the 
reference.  <<Confusing, ain't it.>>

Please tell me if there is a more "correct" solution, or if I'm simply confused to 
oblivion. =)

--- [FIRST CODE SNIPPET] ---

<?
        class Test0_t
        {
                var $oTest1;
                var $oTest2;

                function Test0_t ()
                {
                        $this->oTest1 = new Test1_t($this);
                        $this->oTest2 = new Test2_t();
                }
        }

        class Test1_t
        {
                var $oLnk;

                function Test1_t ( &$_oLnk )
                {
                        $this->oLnk = &$_oLnk;
                }

                function Test ()
                {
                        echo("0:[".$this->oLnk->oTest2->nVal."]<BR>");
                        $this->oLnk->oTest2->nVal++;
                        echo("1:[".$this->oLnk->oTest2->nVal."]<BR>");
                }
        }

        class Test2_t {
                var $nVal;

                function Test2_t ()
                {
                        $this->nVal = 0;
                }
        }

        $oTest0 = new Test0_t();

        $oTest0->oTest1->Test();

        echo("2:[".$oTest0->oTest2->nVal."]<BR>");
?>

--- [SECOND CODE SNIPPET] ---

<?
        class Test0_t
        {
                var $oTest1;
                var $oTest2;

                function Test0_t ()
                {
                        $this->oTest2 = new Test2_t();
                        $this->oTest1 = new Test1_t($this);
                }
        }

        class Test1_t
        {
                var $oLnk;

                function Test1_t ( &$_oLnk )
                {
                        $this->oLnk = &$_oLnk;
                        $this->oLnk->oTest2->Test();
                }

                function Test ()
                {
                        echo("0:[".$this->oLnk->oTest2->nVal."]<BR>");
                        $this->oLnk->oTest2->nVal++;
                        echo("1:[".$this->oLnk->oTest2->nVal."]<BR>");
                }
        }

        class Test2_t {
                var $nVal;

                function Test2_t ()
                {
                        $this->nVal = 0;
                }

                function Test ()
                {
                }
        }

        $oTest0 = new Test0_t();

        $oTest0->oTest1->Test();

        echo("2:[".$oTest0->oTest2->nVal."]<BR>");
?>

Thank you,
-Andrew Immerman

---------------------------------------------------------------------------



ATTENTION! Do NOT reply to this email!
To reply, use the web interface found at http://bugs.php.net/?id=9771&edit=2


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to