php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26143 Call-by-Reference ends in Recursion
Submitted: 2003-11-05 16:33 UTC Modified: 2003-11-06 04:19 UTC
From: claus-poerschke at gmx dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.4 OS: Suse Linux 8.2
Private report: No CVE-ID: None
 [2003-11-05 16:33 UTC] claus-poerschke at gmx dot de
Description:
------------
Implementing a Composite Pattern I get an unexpected 
recursion. If I remove the &-Operator in the aPart::addChild() it 
works. Further comments are made in the code: 

Reproduce code:
---------------
#!/usr/local/bin/php
<?php

class aPart
{
	var $mChildren;
	var $mName;

	function aPart($name)
	{
		$this->mName     = $name;
		$this->mChildren = array();
	}
	function addChild(&$child)
	{
		$this->mChildren[] =& $child;
	}
	function showParts($indent='')
	{
        echo $indent . $this->mName . "\n";
		for ($i = 0;$i < sizeof($this->mChildren);$i++) {
			$this->mChildren[$i]->showParts($indent . '   ');
		}
	}
}

$arrParts = array('bar','baz');

$objP =& new aPart('foo'); // normally i read a template File
$objP1 = $objP; // make Copy - to avoid filesystem Operation!
$objP2 = $objP; // too
$objP1->mName = $arrParts[0]; // assign bar
$objP2->mName = $arrParts[1]; // assing baz
$objP->addChild($objP1); // Add Component 1
$objP->addChild($objP2); // Add Component 2
$objP->showParts(); // show the tree
/*  Prints as expected:
foo
   bar
   baz
*/
// cleanup
unset($objP);
unset($objP1);
unset($objP2);

// try doing the above it another way
$objP =& new aPart('foo');
for ($i=0;$i<sizeof($arrParts); $i++) {
    $objP1 = $objP; // I expected a Copy here :-(
    $objP1->mName = $arrParts[$i];
    $objP->addChild($objP1);
}
$objP->showParts();// Recursion !!!


?>

Expected result:
----------------
Read the comments in the script: 
foo 
   bar 
   baz 

Actual result:
--------------
In the second example i'am getting an recursion 
foo 
   baz 
      baz 
         baz 
            baz 
              ..... 

Patches

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-05 16:44 UTC] moriyoshi@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

A copy of a reference is still a reference.

<?php
$foo = 'huh?';
$a = array(&$foo);
$b = $a;
var_dump($b);
$b[0] = '???';
var_dump($foo);
?>

result:
array(1) {
  [0]=>
  &string(4) "huh?
}
string(3) "???"

 [2003-11-06 04:19 UTC] claus-poerschke at gmx dot de
Shure is a copy of a reference still a reference. 

But I wonder why does my first example works (assigning the copies $objP1 = $objP;$objP2 = $objP;) while the second ends up in a recursion (doing the assignment in a loop). I expected the same behavior. But in the for-loop there seems to be a difference.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Aug 18 20:01:29 2024 UTC