Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

calling static methods using call_user_func #1105

Closed
segy opened this issue Jun 25, 2018 · 11 comments
Closed

calling static methods using call_user_func #1105

segy opened this issue Jun 25, 2018 · 11 comments
Labels
Milestone

Comments

@segy
Copy link

segy commented Jun 25, 2018

Summary of a problem or a feature request

calling static method using call_user_func gives error that it expects callable and is given string / array<int, string>.

Code snippet that reproduces the problem

https://phpstan.org/r/e9f9cd7e41d9a0f4b473d0ec902c762c

Expected output

no error

@ondrejmirtes ondrejmirtes added this to the 0.10 milestone Jul 1, 2018
@integer
Copy link
Contributor

integer commented Jul 19, 2018

Probably same issue as I found for call_user_func and call_user_func_array:

Code snippet that reproduces the problem

https://phpstan.org/r/32d948199501e310faa5fdcd309ab633

Live example

https://3v4l.org/CI130

@JanTvrdik
Copy link
Contributor

The self / static is intentional, because it does not work reliably.
Neither ['self', 'foo'](); or 'self::foo'() will work, making it unsafe to pass those value to functions which require callable.

See the https://wiki.php.net/rfc/consistent_callables for more details about why self, parent and static shall not be used. Instead use for example [static::class, 'foo']().

@JanTvrdik
Copy link
Contributor

The __callStatic error is also intentional. The method MUST be declared either by @method annotation or by providing MethodsClassReflectionExtension.

@ondrejmirtes
Copy link
Member

I plan to add some kind of ExtendedCallableType that will be defined as parameter of call_user_func and call_user_func_array and that will accept these special callables.

@ondrejmirtes
Copy link
Member

I'm gonna close this issue if this RFC passes: https://wiki.php.net/rfc/deprecate_partially_supported_callables

@VincentLanglet
Copy link
Contributor

The RFC passed, but I don't think the issue should be closed

The error https://phpstan.org/r/c1ba1e32-2cc0-49bc-9e33-8de4a1c101b3 seems related to this issue, but according to the RFC call_user_func[$class, $getter] is still valid sometimes. For instance

class A {
	public static function get(): int
	{
		return 1;
	}
}


$a = A::class;
$b = 'get';
var_dump([$a, $b]());

@ondrejmirtes
Copy link
Member

The RFC has not yet passed as the voting closes on 22nd October.

@VincentLanglet Your issue is unrelated and PHPStan's behaviour is expected here. You have no guarantees that [$class, $getter] is a valid callable as the $getter method might not exist. You need to do this instead: https://phpstan.org/r/52f0c45a-5668-4678-a099-949ff3a935ff

Also note that the example you copied here passes: https://phpstan.org/r/f24c3a2f-e190-49f3-b94e-73a314dcc7e2

@VincentLanglet
Copy link
Contributor

@VincentLanglet Your issue is unrelated and PHPStan's behaviour is expected here. You have no guarantees that [$class, $getter] is a valid callable as the $getter method might not exist. You need to do this instead: https://phpstan.org/r/52f0c45a-5668-4678-a099-949ff3a935ff

I see, my bad and thanks.

@phpstan-bot
Copy link
Contributor

@VincentLanglet After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
 PHP 8.0 (1 error)
 ==========
 
-10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array(class-string, string) given.
+10: Parameter #1 $callback of function call_user_func expects callable(): mixed, array{class-string, string} given.
 
 PHP 7.1 – 7.4 (1 error)
 ==========
 
-10: Parameter #1 $function of function call_user_func expects callable(): mixed, array(class-string, string) given.
+10: Parameter #1 $function of function call_user_func expects callable(): mixed, array{class-string, string} given.
Full report

PHP 8.0 (1 error)

Line Error
10 Parameter #1 $callback of function call_user_func expects callable(): mixed, array{class-string, string} given.

PHP 7.1 – 7.4 (1 error)

Line Error
10 Parameter #1 $function of function call_user_func expects callable(): mixed, array{class-string, string} given.

@ondrejmirtes
Copy link
Member

The RFC passed.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants