In Drupal 8 is it possible to register an additional namespace which points to the modules folder in addition to the \Drupal namespace?

e.g.: MyOwnNamespace\{module_name} to point to $path_to_module/src? I am asking this, because I would like to better understand how today's class design will affect my migration process to Drupal 8 one day.

This was a follow up question from my original thread regarding namespacing.

As suggested by another user I split this off into a separate question thread, but the question has been answered in the original thread already.


Splitting my comment over there into a separate answer then ;)

You can, yes, see system_register() and drupal_classloader_register(). Those helper functions hardcode "Drupal\" as they are all about modules, but you could get the class_loader service directly.

As @Clive said there, I would not recommend doing it for your own classes, there isn't really a reason to do so, unless you're including an external library.

External libraries is a different story, that can be useful. As already commented in How to autoload classes from a 3rd party PHP library in Drupal 8, if you do not want to depend on Composer Manager, then you can implement your own autoloader service and register them either by loading a composer-style autoload.php, or if you have it available in a different way, by directly registering it in the cloassloader service.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.