php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #54944 Assignment operator assigns by reference on arrays containing objects.
Submitted: 2011-05-27 19:56 UTC Modified: 2011-05-28 18:21 UTC
From: dtrenz at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.3.6 OS: Mac OSX (Snow Leopard)
Private report: No CVE-ID: None
 [2011-05-27 19:56 UTC] dtrenz at gmail dot com
Description:
------------
When assigning one array to another array that contains a collection of objects, 
the assignment is by reference.

Test script:
---------------
<?php
$obj = (object) null;
$obj->name = 'Joe';

$arrA = array($obj);
$arrB = array();

$arrB = $arrA;

$arrB[0]->name = 'Mary';

print_r($arrA);
print_r($arrB);

Expected result:
----------------
Array
(
    [0] => stdClass Object
        (
            [name] => Joe
        )
 
)

Array
(
    [0] => stdClass Object
        (
            [name] => Mary
        )
 
)

Actual result:
--------------
Array
(
    [0] => stdClass Object
        (
            [name] => Mary
        )
 
)

Array
(
    [0] => stdClass Object
        (
            [name] => Mary
        )
 
)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-05-28 18:21 UTC] johannes@php.net
-Status: Open +Status: Bogus
 [2011-05-28 18:21 UTC] johannes@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

Objects are reference types. For making an copy an explicit clone is needed. PHP internal functions won't do that.
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Mon Mar 16 04:00:02 2026 UTC