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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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: Thu Mar 28 17:01:29 2024 UTC