It is possible to create library package with namespace as below?

namespace Main{
  class PrefixFolderLib1{

Not sure how the JLoader will understand the namespace.

1 Answer 1


Sure. For example, create mylib folder in /library, and a file Myclass.php with the following content:

namespace mylib;

class Myclass

You can register your namespace with

JLoader::registerNamespace('mylib', JPATH_ROOT . '/libraries');

And access your class with \mylib\Myclass. Personally, I create a system plugin to do it automatically:

public function onAfterInitialise()
   JLoader::registerNamespace('mylib', JPATH_ROOT . '/libraries');
  • Thanks. I too initialize using system plugin only. In case if I use multiple namespaces then should I register all the name spaces onAfterInitialise? Commented Nov 17, 2015 at 16:25
  • Yup, you can do it in the same method
    – Marko D
    Commented Nov 17, 2015 at 16:39

Your Answer

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

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