Req #79085 [Opn]: Incosistency on ::class constant

From: Date: Fri, 10 Jan 2020 09:40:06 +0000
Subject: Req #79085 [Opn]: Incosistency on ::class constant
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-224822@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=79085&edit=1 ID: 79085 User updated by: almamu at almamu dot com Reported by: almamu at almamu dot com Summary: Incosistency on ::class constant Status: Open 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: I've tried to come up with situations where that might be useful, but I fail to see any benefits on it. Do you mind sharing an example situation where it is useful? Previous Comments: ------------------------------------------------------------------------ [2020-01-09 21:52:30] requinix@php.net Personally I find it very helpful that I can FOO::class and have it not trigger the autoloader. ------------------------------------------------------------------------ [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

« previous php.bugs (#224822) next »