ID:               9771
 Comment by:       sjs at wsj dot xom
 Reported By:      ahi at visionccg dot com
 Status:           Closed
 Bug Type:         Class/Object related
 Operating System: RedHat 7.0 Linux Standard
 PHP Version:      4.0.4pl1
 New Comment:

http://www.1st-art-gallery.com/index_gallery.htm>Oil Paintings
</A></FONT></SPAN><FONT SIZE="2"> main page<BR>


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

[2004-06-22 20:50:36] yonbi at usanet dot org

<A HREF="http://www.1st-art-gallery.com/home_gallery.htm>Oil Paintings
</A></FONT></SPAN><FONT SIZE="2"> main page<BR>

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

[2001-03-29 06:22:14] [EMAIL PROTECTED]

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.

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

[2001-03-15 14:48:54] ahi at visionccg dot com

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 this bug report at http://bugs.php.net/?id=9771&edit=1

Reply via email to