PHP namespace with Dynamic class name

PHP namespace with Dynamic class name



Wondering if anyone else has encountered this problem when utilizing the new ability to namespace classes using PHP 5.3.



I am generating a dynamic class call utilizing a separate class for defining user types in my application. Basically the class definer takes an integer representation of types and interprets them, returning a string containing the classname to be called as the model for that user.



I have an object model for the user's type with that name defined in the global scope, but I have another object with the same name for the user's editor in the Editor namespace. For some reason, PHP won't allow me to make a namespaced dynamic call as follows.


$definition = Definer::defineProfile($_SESSION['user']->UserType);
new Editor$definition();



The identical syntax works for calling the global basic object model in the global namespace and I use it this way reliably throughout the application.


$definition = Definer::defineProfile($_SESSION['user']->UserType);
new $definition();



This will correctly call the dynamically desired class.



Is there a reason the two would behave differently, or has dynamic calling for namespaces not been implemented in this manor yet as this is a new feature? Is there another way to dynamically call a class from another namespace without explicitly placing its name in the code, but from within a variable?






I would use eval() for that

– yoda
Dec 22 '10 at 20:25




1 Answer
1



Well, just spell out the namespace in the string:


$definition = Definer::defineProfile($_SESSION['user']->UserType);
$class = '\Editor\' . $definition;
$foo = new $class();



And if it's a child namespace (as indicated in the comments), simply prepend the namespace with __NAMESPACE__:


__NAMESPACE__


$class = __NAMESPACE__ . '\Editor\' . $definition;



So if the current namespace is FooBar, and $definition is "Baz", the resulting class would be FooBarEditorBaz


FooBar


$definition


FooBarEditorBaz






Worked perfectly. I get an epic fail for not thinking of just appending this to the string. Thanks!

– DeaconDesperado
Dec 22 '10 at 20:32






No problem (I've hit the same problem before)...

– ircmaxell
Dec 22 '10 at 20:35






Or don't escape the ` for better readability. IMHO. LOL Markdown doesn't work for just a backslash: ` or an escaped backslash: `` (wow now it's very wrong!)

– Rudie
Sep 28 '11 at 18:22



` for better readability. IMHO. LOL Markdown doesn't work for just a backslash:






@Ruddie: I much prefer always escaping the backslash. That way, any time you see a double backslash you know it's a literal backslash, and any time you see a single one you know it's escaping something else after it... Just my experience and preference...

– ircmaxell
Sep 28 '11 at 20:24






This answer is good, but doesn't work for namespaces that are not fully qualified -- ie if the original question had not wanted the leading backslash before Editor (eg if he wanted to reference a child namespace within the current namespace), then applying this answer but without the leading backslash would not have resolved the namespace correctly. Still trying to find a solution to this that doesn't involve me building up the entire fully-qualified-namespace-plus-classname as a string.

– SDC
Oct 30 '12 at 14:56


Editor



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế