Implement PHP class autoloading

Jose Reyero - October 22, 2009 - 16:25
Project:Zend Framework
Version:6.x-1.x-dev
Component:Code
Category:feature request
Priority:normal
Assigned:Unassigned
Status:needs work
Description

This small patch implements PHP autoloading so you just need to use Zend_* classes into your modules, no need to initialize nor anything.

It will work for any class whose name starts with 'Zend_*' and needs PHP > 5.1.2. About PHP autoload, see http://www.php.net/manual/en/function.spl-autoload-register.php

It also gets rid of the global variable, which I don't see why it's needed, and makes it a static one.

AttachmentSize
zend_autoload.patch1.25 KB

#1

Rob Loach - October 22, 2009 - 19:41
Status:active» needs work

Thanks a lot! I love it.

/**
* Autoload implementation
*/
function zend_autoload($class) {dsm($class);
  // Just for classes starting with 'Zend'
  if (strpos($class, 'Zend_') === 0) {
    zend_initialize($class);

We probably don't want that dsm in there ;-) . Applies cleanly to HEAD too!

#2

Rob Loach - October 22, 2009 - 19:44

Actually, we might want to look into Autoload Drupal module and possibly add it as a dependency.

#3

Jose Reyero - October 23, 2009 - 12:10

The issue with Autoload is that you need to define all classes previously (hook_autoload_info), while in this case we are doing class autoloading using only class name.

This patch is compatible with Autoload, actually I'm using both in the same install.

 
 

Drupal is a registered trademark of Dries Buytaert.