|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46310 Namespace resolution is not done when creating objects of a variable class name
Submitted: 2008-10-16 01:28 UTC Modified: 2008-10-16 04:00 UTC
From: benno at transmog dot com dot au Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.0alpha2 OS: Linux, but probably irrelevant
Private report: No CVE-ID: None
 [2008-10-16 01:28 UTC] benno at transmog dot com dot au
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 -
Please advise either way.

Reproduce code:
Classes (defined in namespace ns):

Test suite:

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 />


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2008-10-16 04:00 UTC]
When you do this code:

namespace ns;
$a = new blah;

PHP automatically "expands" it to:

$a = new ns::blah;

The same is true at definition time.  When you do this code:

namespace ns;
class classname{}

PHP automatically expands it to:

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:

class testclass {}
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Feb 04 11:05:48 2023 UTC