eZ Community » Forums » Developer » HOW TO register the autoload of an...
expandshrink

HOW TO register the autoload of an embeded Zend like component in an Extension ?

HOW TO register the autoload of an embeded Zend like component in an Extension ?

Tuesday 10 November 2009 9:12:38 pm - 6 replies

Hi there,

I'm building an extension that will expose a bunch of template operators and modules that user a small component lib i wrote. This component, called Ewm is built using the Zend Coding standard.

I want to embed this lib in an extension and use the lib autoloader to avoid require/includes in operators and module functions

Can't find a way to include the Autoloader code somewhere. Who an i acheave this?

extension
  - ewmadapter
    - autoloads
    - design
    - lib
       - Ewm
    - modules
    - settings
    - translations

the autoloader looks like this (in Ewm/Autoloader.php) :

class Ewm_Autoloader
{
  static public function register()
  {
    ini_set('unserialize_callback_func', 'spl_autoload_call');
    spl_autoload_register(array(new self, 'autoload'));
  }
 
  static public function autoload($class)
  {
    if (0 !== strpos($class, 'Ewm'))
    {
      return false;
    }
 
    require dirname(__FILE__).'/../'.str_replace('_', '/', $class).'.php';
 
    return true;
  }
}

Wednesday 11 November 2009 9:27:43 pm

Hi Sylvain,

the easiest way is probably to let the autoloader of eZ Publish do the job. Active your extension and regenerate the autoload array with the following command line :

php bin/php/ezpgenerateautoloads.php

so your class will be automatically included when needed without having to define a specific Autoloader class.

If your component uses one or more Zend components, you'll also probably need to alter the include_path as there are sometimes explicit include statements in Zend code or include of PHP files not containing a class definition. You can do that in your own code before using your component or in config.php file in the eZ Publish root. I use the former in autostatus extension when using Zend_Service_Twitter component.

Cheers

Thursday 12 November 2009 1:46:50 pm

Or you can set it up in config.php, have a look in config.php-RECOMMENDED file, it includes an example in the bottom.

This means the users of your extensions would need to set this up as part of the extension installation procedure, but should be simple if you provide examples in your installation doc.

Thursday 12 November 2009 8:23:04 pm

I will give a try the the ezpgenerateautoloads.php, did not figured out it will autoload stuff in a extension/xxx/lib folder

does it works for 4.0 ?

Modified on Thursday 12 November 2009 8:30:49 pm by Sylvain Gogel

Thursday 12 November 2009 10:04:18 pm

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

Friday 13 November 2009 9:15:54 pm

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

Works like a charm with ezpgenerateautoload.php !! some ezp Awesomeness ^_^

Monday 20 December 2010 7:19:12 pm

It should work in 4.0 too. In fact, ezpgenerateautoload.php looks for class definitions in all PHP files of each extension.

Btw, I've just discovered that from the 4.1, you can also define and register your own autoload function and register it in config.php but as said by André, it's less handy, if you want to distribute your extension.

Cheers

This means:

// Append your directory to the include path
set_include_path( get_include_path() . PATH_SEPARATOR . 'extensions/xyz/library/');
 
// Just create the new object it, or do a spl_autoload_call( $className );
// to look for the class before you try to initiate it.
$object = new $className();

Modified on Monday 20 December 2010 7:20:26 pm by Olav Frengstad

expandshrink

You must be logged in to post messages in this topic!

36 542 Users on board!

Forums menu

Proudly Developed with from