Jump to content


Photo

Object scope issue in php4


  • Please log in to reply
1 reply to this topic

#1 cyclomaniac

cyclomaniac
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 15 March 2006 - 05:53 PM

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:

<?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>";

?>

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

#2 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,017 posts

Posted 16 March 2006 - 12:09 AM

try

$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>";

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users