Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to save custom autoloaders in zend?

I am trying to use phpThumb in my application by making a custom autoloader.

class My_Loader_Autoloader_PhpThumb implements Zend_Loader_Autoloader_Interface {

   static protected $php_thumb_classes = array(
      'PhpThumb'        => 'PhpThumb.inc.php',
      'ThumbBase'       => 'ThumbBase.inc.php',
      'PhpThumbFactory' => 'ThumbLib.inc.php',
      'GdThumb'         => 'GdThumb.inc.php',
      'GdReflectionLib' => 'thumb_plugins/gd_reflection.inc.php',
   );

  /**
   * Autoload a class
   *
   * @param   string $class
   * @return  mixed
   *          False [if unable to load $class]
   *          get_class($class) [if $class is successfully loaded]
   */
   public function autoload($class) {
      $file = APPLICATION_PATH . '/../library/PhpThumb/' . self::$php_thumb_classes[$class];
      if (is_file($file)) {
         require_once($file);
         return $class;
      }
      return false;
   }
}

i saved this file as PhpThumb.php in the loaders/Autoloader folder. Then added this line to the bootstrap file:

Zend_Loader_Autoloader::getInstance()->pushAutoloader(new My_Loader_Autoloader_PhpThumb());

But it produces an error saying the class was not found. I am guessing that the 'CustomLoader_PhpThumb.php' needs to be saved somewhere else. Any idea guys ?


Update1:

Bootstrap.php file contents

   <?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoload()
    {
        $autoLoader=Zend_Loader_Autoloader::getInstance();
        $resourceLoader=new Zend_Loader_Autoloader_Resource(array(
            'basePath'=>APPLICATION_PATH,
            'namespace'=>'',
            'resourceTypes'=>array(
                'form'=>array(
                    'path'=>'forms/',
                    'namespace'=>'Form_'
                ),
                'models'=>array(
                    'path'=>'models/',
                    'namespace'=>'Model_'
                ),                
            )

            ));

        //return $autoLoader;

        $resourceLoader->addResourceType('loader', 'loaders/', 'My_Loader_');

        $autoLoader->pushAutoloader($resourceLoader);
        $autoLoader->pushAutoloader(new My_Loader_Autoloader_PhpThumb());
    }


}

?>
like image 361
anp Avatar asked Feb 21 '11 09:02

anp


1 Answers

I'm also using PhpThumb and the same autoloader. In my case however it is called My_Loader_Autoloader_PhpThumb and is located in APPLICATION_PATH . '/loaders/Autoloader/PhpThumb.php.

In my Bootstrap.php, first I load the loaders path to the Zend_Loader_Autoloader and then I push the My_Loader_Autoloader_PhpThumb autoloader as follows:

    $autoLoader = Zend_Loader_Autoloader::getInstance();

    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
                'basePath' => APPLICATION_PATH,
                'namespace' => '',
            ));


    $resourceLoader->addResourceType('loader', 'loaders/', 'My_Loader_');

    $autoLoader->pushAutoloader($resourceLoader);
    $autoLoader->pushAutoloader(new My_Loader_Autoloader_PhpThumb());

Hope this will help.

like image 91
Marcin Avatar answered Sep 28 '22 00:09

Marcin