Jump to content

Object scope issue in php4


cyclomaniac

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: []
Link to comment
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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.