9

I was wondering if there is a way to expand an aliased PHP namespace token to get the full namespace identifier. The purpose of doing so is that our object creation factory expects a string with the full namespace so it can autoload it. Here's a quick example:

<?php

use my\namespace\area as MyArea;

$goodObject = MyApp::factory('my\namespace\area\ClassName');
$badObject = MyApp::factory('MyArea\ClassName');

I am looking for some generic solution to be able to expand that NS alias out in any situation, with something equivalent to:

$desiredObject = MyApp::factory(resolve_namespace_alias('MyArea') . '\ClassName');

If anyone out there has tackled this issue, I would love to hear about how you did it.

3
  • I was thinking there might be a way via ReflectionClass however this too is not possible Commented Oct 25, 2011 at 22:57
  • I did try this approach as well, and I guess it's "technically" possible to do by creating a non constructed instance of the object and getting the class name for that. The only problem there is that the code can't be broken out to an external location for re-use since the namespace alias won't be valid in the external context. Commented Oct 25, 2011 at 23:03
  • This approach is all wrong. The main thing that's wrong about it is the static MyApp::factory() - global static factories are a massive antipattern. More pertinently though, the only way to do this is using the approach used by the AliasExpander utility linked below, namely parsing it out of the calling source file, which I hope you agree is a horrible way to do things and really not for production use. The reason it's not possible is because aliases are resolved to their full paths at compile time, and the information has been discarded by run time, which is when you want to access it. Commented May 20, 2013 at 9:43

2 Answers 2

1

I'm not aware to resolve a string, but the class from an object instance:

use my\namespace\area as MyArea;

$b = new MyArea;
$c = get_class($b);
echo $c; # my\namespace\area

This question is somewhat related: Can't get constant from dynamic class using namespaces.

Sign up to request clarification or add additional context in comments.

3 Comments

Constructing a class directly kind of defeats the purpose of the factory ;)
Using get_class() won't work for my needs. The very reason I am needing to resolve the full namespace is to obtain instances of those objects from our various object factories.
I thought that this won't answer your question, so it's not a real answer, this is the closest I came up with. You can not pass a string however to a function that is in some other namespace with it's own aliasing and then expect it to resolve some other namespace's aliases - unless PHP itself provides such a function of which I'm not aware, sorry. I strongly assume no such function exists yet in PHP.
1

Since PHP 5.5 you can use MyArea::class (https://wiki.php.net/rfc/class_name_scalars).

In PHP 5.3+ you can use AliasExpander::expand('MyArea') (https://github.com/milo/utils#aliasexpander).

1 Comment

That alias expander utility uses a truly horrifying approach. I wouldn't use that in production, it would be worse than using reflection for something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.