Ortix Posted July 20, 2014 Share Posted July 20, 2014 I'm trying to implement a library which was written for PSR-0 compliant autoloading in my joomla project. However joomla 2.5 does not support PSR-0 autoloading so I figured that I would have to write my own implementation. Since I want to use my classes across my entire joomla installation I decided to write a plugin which registers the classes onAfterInitialise() but this is where it goes wrong: it overrides joomla's autoloading methods. The library i'm trying to implement is located at libraries/Zoho/CRM/ZohoClient.php with several subfolders. Everything is namespaced with accordance to PSR-0 so starting with: \Zoho\CRM. How do I register the classes in such a way that I can use them globally across joomla? This is my implementation: public function onAfterInitialise() { spl_autoload_register(function ($className) { $className = ltrim($className, '\\'); $fileName = ''; $namespace = ''; if ($lastNsPos = strripos($className, '\\')) { $namespace = substr($className, 0, $lastNsPos); $className = substr($className, $lastNsPos + 1); $fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR; } $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; require $fileName; }); } This obviously wont work because it overrides the joomla autoloading function. I'm running php5.3 (5.4 in 3 months) Quote Link to comment https://forums.phpfreaks.com/topic/290027-register-namespaced-class-in-joomla-25/ Share on other sites More sharing options...
trq Posted July 20, 2014 Share Posted July 20, 2014 This obviously wont work because it overrides the joomla autoloading function It shouldn't. spl_autoload_register adds autoloaders onto a stack of autoloaders. So, unless Joomla is doing something funky with its autoloading, your implementation looks fine. Another option to consider would be to simply use Composer. All you need to do then is include composer's autoloader into your project somewhere. Quote Link to comment https://forums.phpfreaks.com/topic/290027-register-namespaced-class-in-joomla-25/#findComment-1485777 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.