php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #9722 foreach() operates on a copy of an object
Submitted: 2001-03-13 05:09 UTC Modified: 2001-03-13 22:13 UTC
From: johanp at aditus dot nu Assigned:
Status: Closed Package: Class/Object related
PHP Version: 4.0.4pl1 OS: Linux 2.2.16
Private report: No CVE-ID: None
 [2001-03-13 05:09 UTC] johanp at aditus dot nu
The following two array looping statements are not equal
(Assuming Update() updates some instance variables in the 
object)

foreach( $objs as $o )
   $o->Update()


and


for($i=0; $i<count($objs); ++$i)
   $objs[$i]->Update();


In the first case the Update() method will change an anonymous copy of the object in the array while in the second case the update will work on the object in the array. Hence any changes that Update() might do will in the
first case not be done for the objects in the array.

Not sure if this is a genuine bug or just a consequence of the copying semantics for objects in PHP.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-03-13 22:13 UTC] sniper@php.net
Please read the manual:

http://www.php.net/manual/en/control-structures.foreach.php

"Note: Also note that foreach operates on a copy of the specified array, not the array itself, therefore the array pointer is not modified like with the each construct."

--Jani

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Jul 25 01:01:30 2024 UTC