Jump to content

Recommended Posts

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)

 

Link to comment
https://forums.phpfreaks.com/topic/290027-register-namespaced-class-in-joomla-25/
Share on other sites

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.