Jump to content

passing an object reference


praveenhotha

Recommended Posts

Can anyone please explain the following code snippet. I was expecting the output to be 0,1,2,3,4 ?? ???

<?

  class AnyClass

      {

        public $member;

        public function __construct($val)

      {

            $this->member = $val;

      }

    }

   

    $manyObjects = array();

    $obj = null;

   

    for($i = 0; $i < 5;$i++) {

        $obj = new AnyClass($i); // create new instance

        $manyObjects[] = &$obj;

   

    }

   

    for($i = 0; $i < 5;$i++) {

        echo "<br>Value:".$manyObjects[$i]->member."\r\n";

    }

   

?>

 

Output : 4

            4

            4

            4

            4

 

Link to comment
https://forums.phpfreaks.com/topic/114662-passing-an-object-reference/
Share on other sites

Archived

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

×
×
  • 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.