|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2018-08-23 17:47 UTC] marcospassos dot com at gmail dot com
Description: ------------ I'm not sure if it's a bug, but the namespace of an anonymous class is always empty. The behavior for closures is different although. In our case, we would use it as the fully qualified identifier of the class, as we do with closures, but for some reason, this information is lost. Using the file path does not sound like a reasonable alternative. Test script: --------------- https://3v4l.org/qrcUM Expected result: ---------------- Foo\Bar Foo\Bar Actual result: -------------- Foo\Bar PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 15 23:00:01 2025 UTC |
The basic problem is that the class name of anonymous classes isn't prefixed with the namespace. Adding a call to zend_prefix_with_ns() (or doing something like this) in zend_generate_anon_class_name()[1] would almost solve the problem. However, that also would change the result of get_class(), for instance, and although it is document to not rely on the class name of anonymous classes[2], we somehow would have a BC break. Anyhow, there is another problem, namely that the class name may contain the file name where the anonymous class is defined, which is not catered to by ReflectionClass::getNamespaceName() and others, which split the class name at the last backslash. So the following script: <?php $reflectionClass = new \ReflectionClass(new class {}); var_dump($reflectionClass->getShortName()); may return something like: string(17) "76787.php10C7504C" [1] <https://github.com/php/php-src/blob/php-7.3.0beta2/Zend/zend_compile.c#L6283-L6295> [2] <http://php.net/manual/en/language.oop5.anonymous.php> [3] <https://github.com/php/php-src/blob/php-7.3.0beta2/ext/reflection/php_reflection.c#L5156-L5175>I don't think it is a BC break, as the users are aware they should not rely on the class name for anonymous classes. About the second issue, why not to use the same platform-independent approach adopted in closures names? Example: namespace Foo\Bar; may return something like: $reflection = new \ReflectionFunction(new function() {}); echo \get_class(new class); echo $reflection->getName(); "Foo\Bar\{class}" "Foo\Bar\{closure}" In practice, we don't need the brackets as "class" is a reserved word, so there is no way to conflict with an existing class name, but keeping it sounds more consistent.Is there a problem with keeping the current naming scheme and just adding in the namespace? The point is not the name of the class per se but that it should ("should") be a member of the namespace it was defined in.