php.net |  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
Votes:2
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: dave at mudsite dot com
New email:
PHP Version: OS:

 

 [2011-02-01 23:11 UTC] dave at mudsite dot com
Description:
------------
---
From manual page: http://www.php.net/language.namespaces.dynamic
---

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.


Patches

Pull Requests

History

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:

'user'

user.php--
<?php
namespace classes;

class user {

}
 [2013-02-17 10:27 UTC] stas@php.net
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 http://www.php.net/support.php 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] stas@php.net
-Status: Open +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 22 05:01:30 2024 UTC