0

I searched around and can not get an answer, so I am here.

In my PHP file, I import some namespaces, and later, I want to dynamically create one of the imported classes, but it is not working, it seems PHP must need the complete class path to dynamically create class, only the alias of imported namespace will not do it. code sample:
use some\foo; //import foo class
$b = 'foo'; $fullpath_b = 'some\foo';
$fullpath_b::static_function(); //this will work $b::static_function(); //this will not work

my question is, I have only the alias of the imported class 'foo', not the full path class 'some\foo', how can I possibly get the full path name of 'foo', so that I can create the class out of it? If I use new ReflectionClass() to create class, it also need a full path.
note: I do not need to create a object of the class, only need the class.

Thanks for your opinion.

3
  • I don't think it's possible. Probably you need to find another approach - may be pass to $b full namespace or just some flag, depending on which you'll call needed method. Commented Dec 2, 2014 at 22:08
  • thanks! i know that it is not possible now Commented Dec 3, 2014 at 16:30
  • I understand you have the alias of a class. In your case some\foo but you need the real path. I assume your example is not clear enough because both alias and real name are the same. In case your real usecase is much complex, you can take a look on this: stackoverflow.com/questions/19870078/… Commented Jul 28, 2017 at 3:12

1 Answer 1

1

Importing is performed at compile-time and there is currently no way to determine the imported aliases. There's a rfc to add such functionallity to reflection, but it seems this will be rejected.

So you have to use the FQN for the class if you want to use them in Reflection or to create them dynamically (http://php.net/manual/en/language.namespaces.importing.php)

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.