From:             [EMAIL PROTECTED]
Operating system: RedHat 7.0 Linux Standard
PHP version:      4.0.4pl1
PHP Bug Type:     Unknown/Other Function
Bug description:  Passing Multilayered Objects, by Reference, to Independent 
Sub-Objects

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


-- 
Edit Bug report at: http://bugs.php.net/?id=9771&edit=1



-- 
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