php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66961 get_object_vars() returns object references
Submitted: 2014-03-26 14:30 UTC Modified: 2017-10-29 08:51 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: thomas dot stavang dot pedersen at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.5.10 OS: Linux, Fedora 19
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: thomas dot stavang dot pedersen at gmail dot com
New email:
PHP Version: OS:

 

 [2014-03-26 14:30 UTC] thomas dot stavang dot pedersen at gmail dot com
Description:
------------
get_object_vars() returns an array which can be manipulated as you would using a normal object. Manipulating the resulting array should not manipulate the object itself, but it does in this case.



Test script:
---------------
class Foo
{
  public $bar = null;
  public function __construct() {
    $this->bar = new DateTime(); // Now
    $this->far = &$this->bar;
  }
}
$foo= new Foo();
var_dump($foo);
$vars = get_object_vars($foo);
$vars['bar'] = new DateTime('2014-03-25');
var_dump($foo);


Expected result:
----------------
$foo->bar should not be altered, as we are only changing the get_object_vars() array, but it is.

Actual result:
--------------
$foo->bar (and $foo->far) gets a new value.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-07-06 01:50 UTC] yohgaki@php.net
-Status: Open +Status: Verified
 [2014-07-06 01:50 UTC] yohgaki@php.net
http://3v4l.org/H7h4N

It seems PHP does changes value while HHVM does not.
 [2017-10-29 08:51 UTC] jhdxr@php.net
-Status: Verified +Status: Not a bug
 [2017-10-29 08:51 UTC] jhdxr@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

this behavior is expected, it's how reference works in PHP.

the problem here is not related to `get_object_vars()`. it's caused by `$this->far = &$this->bar;`. this will make both `->far` and `->bar` referenced. You can see there is an `&` before `object` in the `var_dump` output.

similar results can be found when you use array (see https://3v4l.org/shrkT).
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC