php - Dynamic namespaced class with alias -
so,
i have issue dynamic object creation using namespaces. here's namespace code:
namespace foo { class bar { } }
now, i'm trying create object of class bar
with:
include('namespace.php'); $sname = 'bar'; $sclass = '\\foo\\'.$sname; $robj = new $sclass; //correct object
and going that. but, want use alias , doing like:
include('namespace.php'); use foo baz; $sname = 'bar'; $sclass0= '\\foo\\'.$sname; $sclass1= '\\baz\\'.$sname; $robj = new $sclass0; //correct object $robj = new $sclass1; //fatal error
and i'm unable instantiate object such way (and accessing via full name still works well). so, question - possible access class via alias somehow, and, if yes, how? i've tried access when using $sclass1='baz\\'.$sname
- no success. also, i've checked declared classes via get_declared_classes()
function, shows have \foo\bar
class (no reference alias).
i'm not sure if matters, i'm using php 5.5 version.
only parser uses namespace aliases canonicalize class references inside each of files.
in other words, doesn't introduce kind of global alias other code can use. once script has been parsed, alias no longer used.
this behaviour described in the manual:
importing performed @ compile-time, , not affect dynamic class, function or constant names.
Comments
Post a Comment