|   | php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | 
| 
  [2008-10-16 01:28 UTC] benno at transmog dot com dot au
 Description: ------------ Suppose you have a namespace that contains: - a function that has "new $blah ();" - the class $blah The instantiation will generate a fatal error if the namespace is not explicitly specified. I assume then that namespace resolution is not done when instantiating an object of a variable class name. This is similar to Bug #45197, but I don't think it's quite the same thing. Classes must know, at runtime, which namespace they belong to for the runtime namespace resolution described in the manual to work - http://php.net/manual/en/language.namespaces.definition.php Please advise either way. Reproduce code: --------------- Classes (defined in namespace ns): http://transmog.com.au/php/ns.phps Test suite: http://transmog.com.au/php/test.phps Expected result: ---------------- a: ok b: ok c: ok Actual result: -------------- a: ok b: ok c: <br /> <b>Fatal error</b>: Class 'testclass' not found in <b>/var/www/ns.php</b> on line <b>7</b><br /> PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits             | |||||||||||||||||||||||||||
|  Copyright © 2001-2025 The PHP Group All rights reserved. | Last updated: Fri Oct 31 05:00:02 2025 UTC | 
When you do this code: <?php namespace ns; $a = new blah; ?> PHP automatically "expands" it to: <?php $a = new ns::blah; ?> The same is true at definition time. When you do this code: <?php namespace ns; class classname{} ?> PHP automatically expands it to: <?php class ns::classname{} ?> In other words, if you want to access "ns::classname" with a dynamic call, you'll need to perform this expansion manually: $a = __NAMESPACE__ . '::' . $classname; return new $a(); $a = "testclass"; $b = new $a(); will always refer to the globally un-namespaced: <?php class testclass {} ?>