From: claus-poerschke at gmx dot de
Operating system: Suse Linux 8.2
PHP version: 4.3.4
PHP Bug Type: *General Issues
Bug description: Call-by-Reference ends in Recursion
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
.....
--
Edit bug report at http://bugs.php.net/?id=26143&edit=1
--
Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=26143&r=trysnapshot4
Try a CVS snapshot (php5): http://bugs.php.net/fix.php?id=26143&r=trysnapshot5
Fixed in CVS: http://bugs.php.net/fix.php?id=26143&r=fixedcvs
Fixed in release: http://bugs.php.net/fix.php?id=26143&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=26143&r=needtrace
Try newer version: http://bugs.php.net/fix.php?id=26143&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=26143&r=support
Expected behavior: http://bugs.php.net/fix.php?id=26143&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=26143&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=26143&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=26143&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=26143&r=php3
Daylight Savings: http://bugs.php.net/fix.php?id=26143&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=26143&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=26143&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=26143&r=float