Skip to content

first class callable conversion #7019

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

Closed
wants to merge 18 commits into from

Conversation

krakjoe
Copy link
Member

@krakjoe krakjoe commented May 20, 2021

@nikic nikic added the RFC label May 20, 2021
@TysonAndre
Copy link
Contributor

Would it make sense to update zend_undefined_function_helper (etc) to indicate that it is creating a closure from an undefined function (etc) rather than calling the function at the time the error is thrown?
(e.g. check if the next opcode is creating a closure - ZEND_CALLABLE_CONVERT,)

The current implementation:

ZEND_VM_COLD_HELPER(zend_undefined_function_helper, ANY, ANY)
{
	USE_OPLINE
	zval *function_name;

	SAVE_OPLINE();
	function_name = RT_CONSTANT(opline, opline->op2);
	zend_throw_error(NULL, "Call to undefined function %s()", Z_STRVAL_P(function_name));
	HANDLE_EXCEPTION();
}
php > $x = ('missing')(...);

Warning: Uncaught Error: Call to undefined function missing() in php shell code:1
Stack trace:
#0 {main}
  thrown in php shell code on line 1
php > $x = Closure::fromCallable('missing');

Warning: Uncaught TypeError: Failed to create closure from callable: function "missing" not found or invalid function name in php shell code:1
Stack trace:
#0 php shell code(1): Closure::fromCallable('missing')
#1 {main}
  thrown in php shell code on line 1

@nikic
Copy link
Member

nikic commented May 25, 2021

@TysonAndre Having a more specific error message would be nice, but it wouldn't be as simple as changing that one place. For example, the error for calling an inaccessible method is generated in the get_method object handler, and I don't think we want to thread information about what kind of "call" it is through there.

Possibly we could make the message more generic so it would work in both situations, for example:

Old: Call to undefined function foobar()
New: Reference to undefined function foobar()

Old: Call to private method Foo::bar() from Baz
New: Reference to private method Foo::bar() from Baz

Though maybe that makes the message in the common case slightly harder to understand.

@mvorisek
Copy link
Contributor

mvorisek commented Jul 5, 2021

Please improve also dump of Closure to contain the specific method/function name. https://3v4l.org/SjSnR

@nikic nikic added this to the PHP 8.1 milestone Jul 9, 2021
@nikic nikic force-pushed the first-class-callable-conversion branch from 5635d27 to 3fcece3 Compare July 13, 2021 13:00
@nikic
Copy link
Member

nikic commented Jul 13, 2021

I added some more test coverage. Anything still missing?

@nikic nikic force-pushed the first-class-callable-conversion branch from 2336613 to 7a3ac9f Compare July 13, 2021 15:13
nikic added 2 commits July 13, 2021 18:44
Does not necessarily hold (e.g. exit in call args).
@nikic nikic force-pushed the first-class-callable-conversion branch from 7a3ac9f to f3af1d4 Compare July 13, 2021 16:45
@nikic nikic closed this in d0b09a7 Jul 14, 2021
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Dec 5, 2022
> Closures for callables can now be created using the syntax `myFunc(...)`, which is identical to `Closure::fromCallable('myFunc')`.

Refs:
* https://www.php.net/manual/en/migration81.new-features.php#migration81.new-features.core.callable-syntax
* https://wiki.php.net/rfc/first_class_callable_syntax
* php/php-src#7019
* php/php-src@d0b09a7

Includes unit tests.
Includes sniff documentation.
jrfnl added a commit to PHPCompatibility/PHPCompatibility that referenced this pull request Dec 5, 2022
> Closures for callables can now be created using the syntax `myFunc(...)`, which is identical to `Closure::fromCallable('myFunc')`.

Refs:
* https://www.php.net/manual/en/migration81.new-features.php#migration81.new-features.core.callable-syntax
* https://wiki.php.net/rfc/first_class_callable_syntax
* php/php-src#7019
* php/php-src@d0b09a7

Includes unit tests.
Includes sniff documentation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants