Bug #80427 [Opn->Nab]: ::class keyword does not work correctly with an imported function

From: Date: Fri, 27 Nov 2020 07:23:55 +0000
Subject: Bug #80427 [Opn->Nab]: ::class keyword does not work correctly with an imported function
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-230664@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=80427&edit=1 ID: 80427 Updated by: requinix@php.net Reported by: udaltsov dot valentin at gmail dot com Summary: ::class keyword does not work correctly with an imported function -Status: Open +Status: Not a bug Type: Bug Package: *General Issues PHP Version: 7.4.13 Block user comment: N Private report: N New Comment: > So ::class just finds the used class and outputs its name regardless of > existence. That's why it doesn't work with functions — it just thinks that > it's > a class in the current namespace. Exactly. More or less. "foo::class" does not try to find "foo" anywhere. There's no autoloading or searching. PHP just expands the name, in-place, according to the current namespace and whatever imports are in place. Obviously it was meant for classes, but because function and constant names follow(ed) the same rules, it could be abused for them as well. Previous Comments: ------------------------------------------------------------------------ [2020-11-26 23:40:45] udaltsov dot valentin at gmail dot com I realized what this phrase means: "The class name resolution using ::class is a compile time transformation". So ::class just finds the used class and outputs its name regardless of existence. That's why it doesn't work with functions — it just thinks that it's a class in the current namespace. Can be closed, my apologies. P.s.: There is a relevant discussion of <function>::func syntax at https://externals.io/message/108459. ------------------------------------------------------------------------ [2020-11-26 23:13:01] udaltsov dot valentin at gmail dot com Description: ------------ Although not documented, class keyword can be used with constants and functions since 5.5: https://3v4l.org/FHYYd. And this is really cool. Unfortunately, myFunction::class and MY_CONSTANT::class do not take use function and use const into account: https://3v4l.org/Vg4tF. It's inconsistent with MyClass::class behavior. I suggest to fix this inconsistency. ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=80427&edit=1

« previous php.bugs (#230664) next »