Forums / Developer / 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 ?

Author Message

Sylvain Gogel

Tuesday 10 November 2009 12:12:38 pm

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;
  }
}

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Damien Pobel

Wednesday 11 November 2009 12: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

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

André R.

Thursday 12 November 2009 4:46:50 am

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.

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Sylvain Gogel

Thursday 12 November 2009 11:23:04 am

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 ?

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Damien Pobel

Thursday 12 November 2009 1: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

Damien
Planet eZ Publish.fr : http://www.planet-ezpublish.fr
Certification : http://auth.ez.no/certification/verify/372448
Publications about eZ Publish : http://pwet.fr/tags/keywords/weblog/ez_publish

Sylvain Gogel

Friday 13 November 2009 12: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 ^_^

--
http://www.ecedi.fr
Agence Web, Créa/Conseils, Accessibilité
eZPublish, Drupal, Zend, Symfony

Olav Frengstad

Monday 20 December 2010 10:19:12 am

"

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();