Jump to content

Archived

This topic is now archived and is closed to further replies.

cyclomaniac

Object scope issue in php4

Recommended Posts

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: []

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.