note 130425 added to language.oop5.constants
| From: | wbeaumo1@gmail.com | Date: | Tue, 22 Jul 2025 20:09:54 +0000 |
| Subject: | note 130425 added to language.oop5.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-222282@lists.php.net to get a copy of this message | ||
The ::class magic constant can be used on any bare text that follows the syntax requirements of a
valid symbol and the symbol will be expanded appropriately regardless of whether it resolves to a
class or even if it exists at all. This means you can use it to expand the names of namespaced
functions and constants (but not class constants or methods) in addition to classes:
<?php
use MyNamespace\MyConstants;
use MyNamespace\MyFunctions;
function_exists(MyFunctions\my_func::class); // The argument expands to
"MyNamespace\MyFunctions\my_func"
defined(MyConstants\MY_CONSTANT::class); // The argument expands to
"MyNamespace\MyConstants\MY_CONSTANT" ?>
And of course it works on symbols in the global namespace:
<?php
namespace MyNamespace;
function_exists(\function_exists::class); // true
defined(\PHP_VERSION::class); // true
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.84.18 (proxied: 23.240.211.150)
----
Manual Page -- https://php.net/manual/en/language.oop5.constants.php
Edit -- https://main.php.net/note/edit/130425
Del: integrated -- https://main.php.net/note/delete/130425/integrated
Del: useless -- https://main.php.net/note/delete/130425/useless
Del: bad code -- https://main.php.net/note/delete/130425/bad+code
Del: spam -- https://main.php.net/note/delete/130425/spam
Del: non-english -- https://main.php.net/note/delete/130425/non-english
Del: in docs -- https://main.php.net/note/delete/130425/in+docs
Del: other reasons-- https://main.php.net/note/delete/130425
Reject -- https://main.php.net/note/reject/130425
Search -- https://main.php.net/manage/user-notes.php