Can I create namespace aliases using PHP 8.1 functions (such as category names)?
We named the default namespace module in Company "Subcompany" and would like it to become "Company" now since its use has expanded.
A perfect answer is to have as little overhead as possible and allow transparent autoloading. One problem is that tools like Intelephense will understand this alias.
This project also uses Composer, so the answer to rewriting the namespace using it will also work.
Yes, and think:
class_alias
was introduced with PHP 5.3 with its new namespace language features. It was created to make it easier to migrate from a global namespace to a namespace - in fact is between namespaces.However, you need to first register a category name for each old class/interface/trait/enumeration to the new counterpart.
The old library has disappeared and now there is a brand new Tiger library. All their classes etc. can be mapped by swapping namespace prefixes (
'My\Old\Lib\Name\' -> 'My\Tiger\Lib\Name'
).This can be done at runtime as PHP does most of the work for you.
Register an autoloader that loads the new namespace prefix based on the old namespace prefix, prefixing the category name with the old name.
Example:
This code is very simple, just two explanations on the registration parameters:
$throw = true
: You want to stop your application immediately when the alias autoloader fails to register, otherwise you'll get weird "class not found" errors much later.$prepend = false
: The callback should only be called if there is still code using the old class name. Therefore, standard code now comes first using the new namespace and standard autoloading.Perhaps worth noting is the
class_exists()
part:If the new class has not been loaded yet, it triggers autoloading.
After registering the autoloader, the old code will automatically load the new class under the old alias.