php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #76028 call_user_func($a) and $a() behave differently
Submitted: 2018-02-28 14:33 UTC Modified: 2021-09-16 12:37 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: bburnichon at gmail dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 7.1 OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
23 + 6 = ?
Subscribe to this entry?

 
 [2018-02-28 14:33 UTC] bburnichon at gmail dot com
Description:
------------
A callable can be defined as an array containing class and method.

Calling call_user_func() with such an array has no issue.
If you try to use the array as a callable, then 'self' loose its special meaning and an error is triggered saying class self does not exists.

Test script:
---------------
<?php

class CallBySelf
{
    public static function foo()
    {
        echo 'Foo', PHP_EOL;
    }
    
    public static function testCall()
    {
        $method = ['self', 'foo'];
        var_dump(is_callable($method));
        
        echo 'Call via call_user_func: ';
        call_user_func($method);
        
        echo 'Direct call: ';
        $method();
    }
}

CallBySelf::testCall();

Expected result:
----------------
Call via call_user_func: Foo
Direct call: Foo


Actual result:
--------------
bool(true)
Call via call_user_func: Foo
Direct call: 
Fatal error: Class 'self' not found in /in/Ssgep on line 19


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-02-28 14:38 UTC] bburnichon at gmail dot com
Checked versions having the issue with 3v4l.org: https://3v4l.org/Ssgep
 [2018-02-28 15:24 UTC] requinix@php.net
-Package: PHP Language Specification +Package: Scripting Engine problem -PHP Version: 5.6.33 +PHP Version: 7.1
 [2018-02-28 17:39 UTC] cmb@php.net
FWIW: don't use the string 'self', but rather self::class,
see <https://3v4l.org/Xkkuf>.
 [2018-02-28 17:54 UTC] bburnichon at gmail dot com
Yes, that's what I've done once I discovered the issue. I just wished I knew this before. I've learnt it the hard way while refactoring an old class which was calling 

```
$method = // Method from user input
$callable = ['self', $method];
if (is_callable($callable)) {
  call_user_func($callable);
}
```

to 

```
$method = // Whitelisted method
$callable = [self::class, $method];
$callable();
```
 [2021-09-16 12:37 UTC] cmb@php.net
-Status: Open +Status: Verified -Type: Bug +Type: Documentation Problem
 [2021-09-16 12:37 UTC] cmb@php.net
> I just wished I knew this before.

So yes, this is actually a documentation problem.

Note that as of PHP 8.1.0 you can use

    self::foo(...)

which is even better, since that callable is callable everywhere.
For previous PHP versions, you can use the more verbose

    Closure::fromCallable([self::class, 'foo'])

to have the same benefit.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Mar 28 18:01:29 2024 UTC