php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #37384 Object Scope within Flow Structures
Submitted: 2006-05-09 15:59 UTC Modified: 2006-05-09 18:12 UTC
From: Anthony dot Cashaw at RoswellPark dot org Assigned:
Status: Not a bug Package: *General Issues
PHP Version: * OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: Anthony dot Cashaw at RoswellPark dot org
New email:
PHP Version: OS:

 

 [2006-05-09 15:59 UTC] Anthony dot Cashaw at RoswellPark dot org
Description:
------------
Should you need to nullify an object created on the fly in the scope of a (for lack of general term) loop when adding those objects to an array?  

foreach and while were tested. I tested even objects that I defined with the class keyword and when the object was instantiated outside of the loop the values that printed where the same at each index.

I'm of the mind that this is how objects work since they're really pointers to a memory address, but then why would the address assigment operator ( &= ) be usefull with objects in cases like this?


Reproduce code:
---------------
<?php

$array = array("one" => 1, "two" => 2, "three" => 3);


foreach($array as $key => $value){


	$MyObject->key = $key;
	$MyObject->value = $value;
	$free[] = $MyObject;
	
        //unless I uncomment the next line there are issues
	//$MyObject= null;
}

echo "<pre>";
print_r($free);
echo "</pre>";
?>

Expected result:
----------------
Array
(
    [0] => stdClass Object
        (
            [key] => one
            [value] => 1
        )

    [1] => stdClass Object
        (
            [key] => two
            [value] => 2
        )

    [2] => stdClass Object
        (
            [key] => three
            [value] => 3
        )

)

Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [key] => three
            [value] => 3
        )

    [1] => stdClass Object
        (
            [key] => three
            [value] => 3
        )

    [2] => stdClass Object
        (
            [key] => three
            [value] => 3
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-05-09 18:12 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC