I am wondering how to load "external" php-libraries into my component.

I would like to fetch mail via imap. To get this working, I would like to use a lib which is able to handle smime-encoded mails.

I got my installer-script working -- it uploads the library to /lib/phpimap.

How can I include this in my component without changing the way the library is written?

As far as I understand, I could use JLoader::discover to do this.

However, if I use:

JLoader::discover('PhpImap', JPATH_LIBRARIES . '/phpimap');

it would "detect" only classes in /lib/phpimap which begin with "PhpImap" - but the classes are named in another way and without any kind of a standard-prefix.

How can I solve that?

  • As you hace noticed Joomla Loader works in a Joomla way, then you have to provide you J classes to load it.
    – Anibal
    Jun 26 '15 at 11:39

As you hace noticed Joomla Loader works in a Joomla way, then you have to provide you J classes to load it.

So, the answer is: just include the library in the same way than it is shown in its examples. A best practice is to create J classes to encapsulate the library calls.

JLoader::discover follows a naming protocol, but JLoader::register allows to register and auto-load specific classes:

JLoader::register('MyClass', .... . '/myclass.php');
  • Hi and thank you for your answer. So I would not use JLoader::discover but jimport? Or do I really use include("/path/to/my/lib/test.php") ?
    – MyFault
    Jun 26 '15 at 12:21
  • You can include specific classes with JLoader::register('MyClass', .... . '/myclass.php');
    – Anibal
    Jun 26 '15 at 12:22
  • Hm okay - unfortunately this does not work at the moment. I got the following setup: d.pr/BrvD But I always get the return: Class "Mailbox" not found :-(
    – MyFault
    Jun 26 '15 at 13:15
  • You have namespaced the Class, then the name of the class (from outside the namespace) is PhpImap\Mailbox. Joomla Loader is compatible with PHP 5.3, a PHP version where there is no namespace support. I would not try to combine them.
    – Anibal
    Jun 26 '15 at 13:20
  • Thanks for your answer - I removed the namespace but it still does not find the Class :-(
    – MyFault
    Jun 26 '15 at 13:48

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.