|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2015-09-25 14:18 UTC] 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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 20:00:02 2025 UTC |
Ok. So this is basically a "won't fix"? Then I guess I'll have to do it like before PHP 5.5 trait GetClassNameTrait { public static function getClassName() { return get_called_class(); } }