php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80427 ::class keyword does not work correctly with an imported function
Submitted: 2020-11-26 23:13 UTC Modified: 2020-11-27 07:23 UTC
From: udaltsov dot valentin at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.4.13 OS:
Private report: No CVE-ID: None
 [2020-11-26 23:13 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-26 23:40 UTC] 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-27 07:23 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-11-27 07:23 UTC] requinix@php.net
> 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.
 
PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Wed Jul 06 02:03:41 2022 UTC