cyclomaniac Posted March 15, 2006 Share Posted March 15, 2006 The following code works fine in php5. Unfortunately, I'm deploying to a php4 environment. I'm hoping someone can point to my silly mistake so I can move forward.Code:[code]<?php$globalBar = NULL;class OuterClass { var $foos = array(); function OuterClass() { for ($i = 0; $i < 3; $i++){ $fooObj = new Foo($i); $this->foos[] = $fooObj; for ($j = 0; $j < 3; $j++) { $barObj = new Bar($j); $fooObj->addBar($barObj); $globalBar = $barObj; } echo "Current Bar Count: [" . count($fooObj->bars) . "]<br>"; } } }class Foo { var $fooNum; var $bars = array(); function Foo ($foo) { $this->fooNum = $foo; } function addBar (& $barObj) { $this->bars[] = $barObj; }}class Bar { var $barNum; function Bar($bar) { $this->barNum = $bar; }}$outerClass = new OuterClass();$numFoo = count($outerClass->foos);for ($i = 0; $i < $numFoo; $i++ ) { $currFoo = $outerClass->foos[$i]; $numBar = count($currFoo->bars); echo "Foo number: [$currFoo->fooNum] has the following number" . "of bars: [$numBar]<br>"; for ($j = 0; $j < $barNum; $j ++) { $currBar = $currFoo->bars[$j]; echo "Bar number: [$currBar->barNum]<br>"; }}echo "Global Bar: [$globalBar->barNum]<br>";?>[/code]Produces the following output... I lose the bars when I leave the scope of the for loop in OuterClass' constructor.Current Bar Count: [3]Current Bar Count: [3]Current Bar Count: [3]Foo number: [0] has the following numberof bars: [0]Foo number: [1] has the following numberof bars: [0]Foo number: [2] has the following numberof bars: [0]Global Bar: [] Quote Link to comment Share on other sites More sharing options...
Barand Posted March 16, 2006 Share Posted March 16, 2006 try[code]$globalBar = NULL;class OuterClass { var $foos = array(); function OuterClass() { for ($i = 0; $i < 3; $i++){ $fooObj = & new Foo($i); $this->foos[] = &$fooObj; for ($j = 0; $j < 3; $j++) { $barObj = new Bar($j); $fooObj->addBar($barObj); $globalBar = $barObj; } echo "Current Bar Count: [" . count($fooObj->bars) . "]<br>"; } }}class Foo { var $fooNum; var $bars = array(); function Foo ($foo) { $this->fooNum = $foo; } function addBar (& $barObj) { $this->bars[] = $barObj; }}class Bar { var $barNum; function Bar($bar) { $this->barNum = $bar; }}$outerClass = new OuterClass();$numFoo = count($outerClass->foos);for ($i = 0; $i < $numFoo; $i++ ) { $currFoo = &$outerClass->foos[$i]; $numBar = count($currFoo->bars); echo "Foo number: [$currFoo->fooNum] has the following number" . "of bars: [$numBar]<br>"; for ($j = 0; $j < $numBar; $j ++) { $currBar = &$currFoo->bars[$j]; echo "Bar number: [$currBar->barNum]<br>"; }}echo "Global Bar: [$globalBar->barNum]<br>";[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.