|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2003-11-05 16:44 UTC] moriyoshi@php.net
[2003-11-06 04:19 UTC] claus-poerschke at gmx dot de
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Dec 02 08:00:01 2025 UTC |
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 .....