php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Undocumented change: order of returned parameters in get_object_vars() differs from previous versions

Open beephotography opened this issue 4 years ago • 3 comments

Description

The following code:

<?php
class TestA extends TestB
{
    protected $foo;

    public function test()
    {
        return get_object_vars($this);
    }
}

class TestB
{
    protected $bar;
}

$foo = new TestA();
var_dump($foo->test());

Resulted in this output:

array(2) {
  ["bar"]=>
  NULL
  ["foo"]=>
  NULL
}

But in PHP < 8.1.0 I got this output instead:

array(2) {
  ["foo"]=>
  NULL
  ["bar"]=>
  NULL
}

PHP Version

PHP >= 8.1.0

Operating System

No response

beephotography avatar Jan 26 '22 09:01 beephotography

I can't see any recent changes in get_object_vars that would explain this. Maybe it has something to do with Dmitrys changes about inheritance? The order doesn't seem to be documented as of yet, so it might be best to simply update the documentation accordingly.

iluuu1994 avatar Jan 26 '22 09:01 iluuu1994

@iluuu1994 @beephotography maybe it's covered here https://www.php.net/manual/en/migration81.other-changes.php#migration81.other-changes.functions.core

The order of properties used in foreach, var_dump(), serialize(), object comparison, etc. was changed. Properties are now ordered naturally according to their declaration and inheritance. Properties declared in a base class are going to be before the child properties.

pospjan avatar Jan 26 '22 17:01 pospjan

@pospjan Yes, thank you!

iluuu1994 avatar Jan 26 '22 20:01 iluuu1994