#26143 [Bgs]: Call-by-Reference ends in Recursion
| From: | claus-poerschke at gmx dot de | Date: | Thu, 06 Nov 2003 09:19:47 +0000 |
| Subject: | #26143 [Bgs]: Call-by-Reference ends in Recursion | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-50069@lists.php.net to get a copy of this message | ||
ID: 26143
User updated by: claus-poerschke at gmx dot de
Reported By: claus-poerschke at gmx dot de
Status: Bogus
Bug Type: *General Issues
Operating System: Suse Linux 8.2
PHP Version: 4.3.4
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2003-11-05 16:44:21] 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-05 16:33:28] 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
.....
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=26143&edit=1