Package home | Report new bug | New search | Development Roadmap Status: Open | Feedback | All | Closed Since Version 1.2.0

Bug #14307 addNode() & references
Submitted: 2008-07-08 07:31 UTC
From: janjonas1 Assigned:
Status: Bogus Package: Structures_Graph (version 1.0.2)
PHP Version: 5.2.6 OS: Linux
Roadmaps: (Not assigned)    
Subscription  
Comments Add Comment Add patch


Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know! Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
MUST BE VALID
Solve the problem : 47 - 34 = ?

 
 [2008-07-08 07:31 UTC] janjonas1 (Jan Jonas)
Description: ------------ If you are using addNode($node) passing two or more times the same variable "overwrites" previously added nodes. This behaviour is not intuitive and erroneous when using addNode in loops constructs (see example) or recursions. Test script: --------------- $graph = new Structures_Graph(); for ($i=0; $i<=1; $i++) { $node = new Structures_Graph_Node(); $node->setData($i); $graph->addNode($node); } $nodesInGraph = $graph->getNodes(); foreach ($nodesInGraph as $nodeInGraph) { echo $nodeInGraph->getData().'<br/>'; } Expected result: ---------------- 0 1 Actual result: -------------- 1 1

Comments

 [2009-05-03 15:39 UTC] oscarjd74 (Oscar Dijkhoff)
@janjonas This is not a bug. You should assign the variables by reference. Like so: Test script: --------------- $graph =& new Structures_Graph(); for ($i=0; $i<=1; $i++) { $node =& new Structures_Graph_Node(); $node->setData($i); $graph->addNode($node); } $nodesInGraph = $graph->getNodes(); foreach ($nodesInGraph as $nodeInGraph) { echo $nodeInGraph->getData().'<br/>'; } Expected result: ---------------- 0 1 Actual result: -------------- 0 1
 [2010-03-08 20:01 UTC] doconnor (Daniel O'Connor)
-Status: Open +Status: Bogus
As much as it pains me to say it, clone or reference stuff is the way to go here. "Fixing" it would break it for PHP 4 users, and get me shot.