php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9771 Passing Multilayered Objects, by Reference, to Independent Sub-Objects
Submitted: 2001-03-15 14:48 UTC Modified: 2001-03-29 06:22 UTC
From: ahi at visionccg dot com Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.4pl1 OS: RedHat 7.0 Linux Standard
Private report: No CVE-ID: None
 [2001-03-15 14:48 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-29 06:22 UTC] stas@php.net
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.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 06:01:29 2024 UTC