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.
ReflectionClasshowever this too is not possibleMyApp::factory()- global static factories are a massive antipattern. More pertinently though, the only way to do this is using the approach used by theAliasExpanderutility 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.