php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77877 call_user_func() passes $this to static methods
Submitted: 2019-04-11 10:34 UTC Modified: 2019-04-11 18:23 UTC
From: dmitry@php.net Assigned: dmitry (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 7.1Git-2019-04-11 (Git) OS: *
Private report: No CVE-ID: None
 [2019-04-11 10:34 UTC] dmitry@php.net
Description:
------------
call_user_func([$obj,"static_method"]) actually performs non-static call.

Most other internal functions that use callbacks (e.g. array_map) are not affected.

Test script:
---------------
<?php
class Foo {
        static public function bar() {
                var_dump($this);
        }
}
try {
        array_map([new Foo, 'bar'],[1]);
} catch (Throwable $e) {
        echo $e->getMessage() . "\n";
}
try {
        call_user_func([new Foo, 'bar']);
} catch (Throwable $e) {
        echo $e->getMessage() . "\n";
}
?>


Expected result:
----------------
Using $this when not in object context
Using $this when not in object context

Actual result:
--------------
Using $this when not in object context
object(Foo)#3 (0) {
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-04-11 10:34 UTC] dmitry@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: dmitry
 [2019-04-11 10:53 UTC] daverandom@php.net
-Summary: call_user_func() passes $this to satatic methods +Summary: call_user_func() passes $this to static methods
 [2019-04-11 11:31 UTC] dmitry@php.net
Automatic comment on behalf of dmitry@zend.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=a1b7ccdfc69a1ad9938039f687ad7059ed1bd506
Log: Fixed bug #77877 (call_user_func() passes $this to satatic methods).
 [2019-04-11 11:31 UTC] dmitry@php.net
-Status: Assigned +Status: Closed
 [2019-04-11 11:32 UTC] me at mega6382 dot me
To me this seems to be:

call_user_func([new Foo, 'bar']);

Equivalent to:

$obj = new Foo;
$obj->bar(); 

Both of these performs non-static call, which I think is how it should be. 

But if you write it like this:

call_user_func(['Foo', 'bar']);

which is equivalent to:

Foo::Bar();

It performs static call. which I think how is it should be.
 [2019-04-11 18:23 UTC] dmitry@php.net
If bar() declared as static, the following code makes a static call.

$obj = new Foo;
$obj->bar(); 

call_user_func() was fixed to do the same.
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Tue Aug 20 05:01:27 2019 UTC