php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #73715 Undocumented change in 7.0 of call_user_func + get_object_vars
Submitted: 2016-12-11 01:05 UTC Modified: 2016-12-15 15:22 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: love at sickpeople dot se Assigned:
Status: Open Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: love at sickpeople dot se
New email:
PHP Version: OS:

 

 [2016-12-11 01:05 UTC] love at sickpeople dot se
Description:
------------
In PHP 5 it was possible to invoke get_object_vars() with call_user_func() in order to get only public variables. This is no longer the case.

 1) I can't find any documentation about this change on the pages for get_object_vars, call_user_func OR the migration for 7.0 (grepping for 'scope')

 2) This upvoted comment introduced the hack with call_user_func() to me: http://php.net/manual/en/function.get-object-vars.php#113435


I suggest two things

 A) Add an editor note on the comment mentioned in 2). Tell that this only works in PHP 5

 B) Add more info in http://php.net/manual/en/migration71.incompatible.php or http://php.net/manual/en/migration71.changed-functions.php

  - I presume this is not a change in call_user_func() or get_object_vars() themselves. I was not able to find what the source of this change actually is, hence more info of *some kind* is requested.


Test script:
---------------
class M
  {
    protected $A = 'foo';

    private $B = 'bar';

    public Function dump ()
      {
        var_dump (
            array_keys (get_object_vars ($this)),
            array_keys (call_user_func ('get_object_vars', $this))
          );

        return $this;
      }

  }

 var_dump (get_object_vars ((new M)->dump ()));

Expected result:
----------------
https://3v4l.org/94ES3

 - The result between PHP 5 and PHP 7 differs


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-12-11 01:09 UTC] love at sickpeople dot se
I posted the wrong links to the migration. I have verified that I checked the guide for 7.0 in 1).

Correct links

http://php.net/manual/en/migration70.incompatible.php

http://php.net/manual/en/migration70.changed-functions.php
 [2016-12-11 16:04 UTC] cmb@php.net
The behavioral change *might* have been introduced with commit de306e70[1] and *might* not even have been intentional.

[1] <http://git.php.net/?p=php-src.git;a=commit;h=de306e70882dd7dd04ea952ef3c665304837c3be>
 [2016-12-15 15:22 UTC] bwoebi@php.net
@cmb: It was not intentional to have different behaviors between when call_user_func() is using the function vs the opcode in 7.0. (see https://3v4l.org/kuV9o)

That was fixed in 7.1; and now call_user_func() will always preserve scope.

A workaround instead of call_user_func() would be:
(function($obj) { return get_object_vars($obj); })->call(new class {}, $this)
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 19:01:29 2024 UTC