Skip to content

The ability to call non-static methods statically has been removed. #5782

@mglaman

Description

@mglaman

Bug report

From #5368

Thus is_callable() will fail when checking for a non-static method with a classname (must check with an object instance).

PHPStan also does not error when using call_user_func.

Comment in parent issue:

Should be handled in ConstantArrayType::getCallableParametersAcceptors();

Actually, the following does error

HelloWorld::sayHello('FOO');

But not when call_user_func is invoked.

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class HelloWorld
{
    public function sayHello(string $date): void
    {
        echo 'Hello, ' . $date;
    }
}

if (is_callable([HelloWorld::class, 'sayHello'])) {
    // always false.
}
if (is_callable('HelloWorld::sayHello')) {
    // always false.
}

$hello = new HelloWorld();
if (is_callable([$hello, 'sayHello'])) {
    // always true.
}

// Valid
$hello->sayHello('ABC');
call_user_func([$hello, 'sayHello'], 'foo');

// Will error
call_user_func([HelloWorld::class, 'sayHello']);

https://phpstan.org/r/12fd1cbe-3da1-4885-8328-748a21f94235

Expected output

The is_callable to non-static methods should say always returns false.

And the call_user_func statically should report

Argument #1 ($callback) must be a valid callback, non-static method HelloWorld::sayHello() cannot be called statically

Did PHPStan help you today? Did it make you happy in any way?

It sure did!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions