praveenhotha Posted July 14, 2008 Share Posted July 14, 2008 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 Quote Link to comment Share on other sites More sharing options...
MiCR0 Posted July 14, 2008 Share Posted July 14, 2008 Error with : $manyObjects[] = &$obj; Should be $manyObjects[] = $obj; Quote Link to comment Share on other sites More sharing options...
praveenhotha Posted July 14, 2008 Author Share Posted July 14, 2008 It doesn't throw any error, but prints five 4s. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.