note 116995 deleted from language.oop5.basic by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 18:16:33 +0000 |
| Subject: | note 116995 deleted from language.oop5.basic by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207270@lists.php.net to get a copy of this message | ||
Note Submitter: igor dot pellegrini at diespam-berlinonline dot de
----
It should be noted that the new keyword "::class", added in 5.5, WILL NOT PERFORM ANY
CHECK on the existence of the targeted class, either in the current namespace or another.
If the targeted class has not an explicitly defined namespace the keyword will JUST PROVIDE a fully
qualified name corresponding to:
CURRENT_NAMESPACE + TARGET_CLASS_NAME
This could NOT correspond to any existing class!
In PHP >5.x it is possible to 'USE' a non-existing namespace and/or a non-existing
class, as long as the class is not really used in the code.
The interpreter will NOT raise any error in these case:
- the USEd namespace doesn't exist
<?php
use This\Is\Just\A\Fake\Namespace\SomewhereExistantClass as NotFoundClass;
?>
- the class doesn't exist in the defined namespace
<?php
use In\This\Valid\Namespace\Doesnt\Exist\The\GhostClass as NonExistentClass;
?>
- the class doesn't exist in the current namespace (when not specifying the class with the
'USE' keyword)
<?php
use This\Is\A\Valid\Namespace\GhostClass as NonExistentClass;
?>
So doing something like:
<?php
namespace MyProject;
use MyProject\WrongNamespace\NonExistingClassName;
class Test
{
public static function getNonExistantClass()
{
return NonExistingClassName::class;
}
}
echo Test::getNonExistantClass();
?>
will print
"MyProject\WrongNamespace\NonExistingClassName"
WITHOUT raising any error!
----
So, to recap:
To avoid tricky side effects, IF you want to USE THE '::CLASS' keyword, ALWAYS REMEMBER TO
carefully CHECK IT HAS BEEN DECLARED CORRECTLY in the 'USE' statement (that actually
exists in the current or in the defined namespace)