Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Best way to autoload PCR-O standard vendors
  • I using some Zend classes with Fuel, and i autoad zend with Zend Autoloader like this:
    require_once VENDORPATH.'/Zend/Loader/StandardAutoloader.php';
    $loader = new Zend\Loader\StandardAutoloader();
    $loader->register();
    

    After this when php autoload classes from Fuel, it try to do it with Zend Autoloader too. This is bad for performance, so i add my own autoader for zend
    class MyZendAutoloader extends Zend\Loader\StandardAutoloader
    {
     
     public function autoload($class)
     {
         if (substr($class, 0, 4) != 'Zend')
          return false;
           
      return parent::autoload($class);
     }
    }
    

    Other way is to use Fuel autoloader only, but then we need to map every class like this:
    Autoloader::add_namespace('Zend', VENDORPATH.'Zend', true);
    Autoloader::add_classes(array(
     // Add classes you want to override here
     'Zend\Acl\Acl'            => VENDORPATH.'Zend/Acl/Acl.php',
     'Zend\Acl\Role'           => VENDORPATH.'Zend/Acl/Role.php',
     'Zend\Acl\Resource'          => VENDORPATH.'Zend/Acl/Resource.php',
     'Zend\Acl\Exception'          => VENDORPATH.'Zend/Acl/Exception.php',
     'Zend\Acl\Exception\InvalidArgumentException'    => VENDORPATH.'Zend/Acl/Exception/InvalidArgumentException.php',
     'Zend\Acl\Exception\RuntimeException'      => VENDORPATH.'Zend/Acl/Exceptio/RuntimeException.php',
     'Zend\Acl\Role\GenericRole'        => VENDORPATH.'Zend/Acl/Role/GenericRole.php',
     'Zend\Acl\Role\Registry'         => VENDORPATH.'Zend/Acl/Role/Registry.php',
     
    )); 
    
    Does first way with Zend Autoloader heavy affecting performance ? What is your way to load PCR-0 vendors, with CamelCase filenames ?
  • You can add a PSR-0 compliant searchpath to the autoloader using
    \Autoloader::add_namespace('Zend', '/my/path/to/zend', true);
    

    I've never used it. You might have to do this for every namespace used in the package, I'm not sure how smart the autoloader is in this respect.
  • Harro Verton wrote on Tuesday 24th of January 2012:
    You can add a PSR-0 compliant searchpath to the autoloader using
    \Autoloader::add_namespace('Zend', '/my/path/to/zend', true);
    

    I've never used it. You might have to do this for every namespace used in the package, I'm not sure how smart the autoloader is in this respect.

    Which respect? With Zend autoloader, or Fuel's autoloader ? I use Fuel's autoloader and PSR-0 compliant searchpath. This approach weakness is we need to map every Zend class, and you know that zend have tons of classes :)
  • I wrote "add the namespace", not "add the classes". You don't need to add every class file. FuelPHP's autoloader is very capable when it comes to translating namespaces and classnames to file paths. All it needs is to know where things live, and in which format. The code I gave was the definition of the namespace and it's location, and a flag (the third parameter) that indicates that this is a PSR-0 namespace. After this, the FuelPHP autoloader should be able to map "\Zend\Acl\VeryLongClassName" to "/my/path/to/zend/Acl/VeryLongClassName.php" without requiring additional code.
  • I also missed to add DIRECTORY_SEPARATOR at the and of vendor path, so the right code is
    \Autoloader::add_namespace('Zend', '/my/path/to/zend'.DS, true);
    

    Thanks, this is very helpful. I thought we need to map every class.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion