note 116995 added to language.oop5.basic

From: Date: Tue, 31 Mar 2015 13:27:19 +0000
Subject: note 116995 added to language.oop5.basic
Groups: php.notes 
Request: Send a blank email to php-notes+get-202099@lists.php.net to get a copy of this message
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) ---- Server IP: 72.52.91.14 Probable Submitter: 85.183.206.181 (proxied: 195.74.70.150) ---- Manual Page -- http://php.net/manual/en/language.oop5.basic.php Edit -- https://master.php.net/note/edit/116995 Del: integrated -- https://master.php.net/note/delete/116995/integrated Del: useless -- https://master.php.net/note/delete/116995/useless Del: bad code -- https://master.php.net/note/delete/116995/bad+code Del: spam -- https://master.php.net/note/delete/116995/spam Del: non-english -- https://master.php.net/note/delete/116995/non-english Del: in docs -- https://master.php.net/note/delete/116995/in+docs Del: other reasons-- https://master.php.net/note/delete/116995 Reject -- https://master.php.net/note/reject/116995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#202099) next »