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: [] Link to comment https://forums.phpfreaks.com/topic/5050-object-scope-issue-in-php4/ 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] Link to comment https://forums.phpfreaks.com/topic/5050-object-scope-issue-in-php4/#findComment-17981 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.