|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #53904 Use of dynamic class name in namespace construction
Submitted: 2011-02-01 23:11 UTC Modified: 2013-02-17 10:27 UTC
Avg. Score:4.0 ± 1.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: dave at mudsite dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.5 OS: Linux x86
Private report: No CVE-ID: None
 [2011-02-01 23:11 UTC] dave at mudsite dot com
From manual page:

When not using namespaces the following code works just fine

Function loadClass ($class) {
    $this->obj = new $class();

However, when attempting to implement namespaces:

function loadClass ($class) {
    $this->obj = \classes\$class();

Throws an "Parse error: syntax error, unexpected T_VARIABLE, expecting 
T_STRING".  Which is kind of silly, since the corrected function looks like:

function loadClass ($class) {
    $class = '\classes\\'.$class;
    $this->obj = new $class();

Upon reading other bugs that query about using dynamic content in namespaces, 
I'm confused as to why the latter example above actually does work if namespaces 
are computed on compile time, when the class isn't known at compile time.

Expected result:
I kindly request that you be able to use dynamic elements when using namespaces.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2011-02-01 23:12 UTC] dave at mudsite dot com
I guess I should note, that the variable coming into this function would be 
something like:


namespace classes;

class user {

 [2013-02-17 10:27 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

You can use __NAMESPACE__ for dynamically constructing namespaced names.
 [2013-02-17 10:27 UTC]
-Status: Open +Status: Not a bug
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 05 10:01:31 2024 UTC