Bug #66961 [Ver->Nab]: get_object_vars() returns object references
| From: | jhdxr@php.net | Date: | Sun, 29 Oct 2017 08:51:22 +0000 |
| Subject: | Bug #66961 [Ver->Nab]: get_object_vars() returns object references | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-212354@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=66961&edit=1
ID: 66961
Updated by: jhdxr@php.net
Reported by: thomas dot stavang dot pedersen at gmail dot com
Summary: get_object_vars() returns object references
-Status: Verified
+Status: Not a bug
Type: Bug
Package: Unknown/Other Function
Operating System: Linux, Fedora 19
PHP Version: 5.5.10
Block user comment: N
Private report: N
New Comment:
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).
Previous Comments:
------------------------------------------------------------------------
[2014-07-06 01:50:27] yohgaki@php.net
http://3v4l.org/H7h4N
It seems PHP does changes value while HHVM does not.
------------------------------------------------------------------------
[2014-03-26 14:30:16] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=66961&edit=1