Bug->Req #79085 [Nab->Opn]: Incosistency on ::class constant
Edit report at https://bugs.php.net/bug.php?id=79085&edit=1
ID: 79085
Updated by: requinix@php.net
Reported by: almamu at almamu dot com
Summary: Incosistency on ::class constant
-Status: Not a bug
+Status: Open
-Type: Bug
+Type: Feature/Change Request
Package: Class/Object related
Operating System: Any
PHP Version: 7.2.26
Block user comment: N
Private report: N
New Comment:
Personally I find it very helpful that I can FOO::class and have it not trigger the autoloader.
Previous Comments:
------------------------------------------------------------------------
[2020-01-09 11:57:33] almamu at almamu dot com
I understand that's how it works internally, that's why I did create this as
Feature/Change request and not as a bug. I think this should behave consistently as other constants
(because the documentation calls it a special constant: "The special ::class constant is
available as of PHP 5.5.0, and allows for fully qualified class name resolution at compile time,
this is useful for namespaced classes: "), just like there's a request to make it work
with objects: https://wiki.php.net/rfc/class_name_literal_on_object
This RFC makes an important point too, we already support static::class, which is resolved on
runtime and not compiletime.
------------------------------------------------------------------------
[2020-01-08 19:38:39] requinix@php.net
::class is special and not really a constant. The compiler sees it and expands the
"FOO::class" to be a string. As in when that gets executed, it's a literal string
value - like if you had written it right into the code yourself.
------------------------------------------------------------------------
[2020-01-08 19:18:05] almamu at almamu dot com
Description:
------------
I've noticed some inconsistencies on how the ::class magic constant is handled compared to
other constants/class members. As seen in the example script, if you try to access ::class on a
non-existant class, you don't get any kind of error, which would be expected, as the class
itself doesn't exist. This can provoke some nasty errors where you typo the class' name
and PHP doesn't even give you a (expected) warning.
I've created an interactive example with that same code so you can check it live and toy with
it: https://3v4l.org/RbIhK
Test script:
---------------
<?php
echo FOO::class;
echo FOO::constantName;
echo FOO::$username; // It works as echoing Typo\WTF; It shall fail...
Expected result:
----------------
Fatal error: Uncaught Error: Class 'FOO' not found in /in/RbIhK:2
Stack trace:
#0 {main}
thrown in /in/RbIhK on line 3
Process exited with code 255.
Actual result:
--------------
FOO
Fatal error: Uncaught Error: Class 'FOO' not found in /in/RbIhK:3
Stack trace:
#0 {main}
thrown in /in/RbIhK on line 3
Process exited with code 255.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79085&edit=1
Thread (8 messages)