Edit report at https://bugs.php.net/bug.php?id=70582&edit=1
ID: 70582
Updated by: requinix@php.net
Reported by: andreas dot stangl at stansotec dot de
Summary: Referncing NotExistingClass::class should trigger
error/warning/notice
-Status: Open
+Status: Not a bug
-Type: Feature/Change Request
+Type: Bug
Package: Class/Object related
Operating System: Any
PHP Version: Next Major Version
Block user comment: N
Private report: N
New Comment:
::class is just a fancy way of getting the class's fully-qualified name without having to
hard-code it into your application. It's a step-up from having to write
"My\\FancyClass". I suggest using an IDE which can do things like search for symbols (and
not just mere strings) because that means that before you rename a class you can search for its old
name and will find the FancyClass::class usage - which wouldn't be as quick with the class name
as a string.
To the point, PHP does not need to autoload the class to determine its name: the only thing that
affects the name is the current namespace and any use declarations present, which it already has on
hand. Autoloading would unnecessarily slow down execution.
Regarding the inconsistency, all magic constants have special behavior - that's what makes them
"magic". __FILE__ and __LINE__ and such are magical in that their values are not constant,
and if you try to compare them in two different places then you won't get the regular behavior
(equality). ::class is magical in that it is not literally defined on the class, and if you try to
use it on a class that does not exist then you won't get the regular behavior (a fatal error).
Previous Comments:
------------------------------------------------------------------------
[2015-09-25 14:24:18] andreas dot stangl at stansotec dot de
Changed "Package" to "Class/Object related".
------------------------------------------------------------------------
[2015-09-25 14:18:07] andreas dot stangl at stansotec dot de
Description:
------------
When referencing the "magic" class constant "::class" the interpreter does not
care if the given class does not exist.
If I have e.g. a class "My\FancyClass" and I access the "class" constant like
echo My\FancyClass::class;
I get
"My\FancyClass"
(which is pretty cool especially for unit tests). But if e.g. My\FancyClass gets renamed to
"My\FoobarClass" the interpreter will not complain at all.
But referencing any other (not existing) constant of a not existing class like
echo My\FoobarClass::blah;
Produces a fatal error. This looks a little inconsistent to me.
Test script:
---------------
<?php
namespace My;
class FancyClass {}
echo FancyClass::class . "\n"; // useful
echo NotExistingClass::class . "\n"; // imho not so useful :-/
echo NotExistingClass::blah; // fatal error
Expected result:
----------------
My\FancyClass
PHP Fatal error: Class 'My\NotExistingClass' not found in testScript.php on line 7
Actual result:
--------------
My\FancyClass
My\NotExistingClass
PHP Fatal error: Class 'My\NotExistingClass' not found in testScript.php on line 8
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70582&edit=1