(on Friday, 31 July 2009, 09:02 AM +0200):
> Hi everyone, I have a recurring issue with the autoloader which can be
> easily addressed by using the Fallback AutoLoader, thing I would like
> to avoid.
>
> Many external libraries like Doctrine have a main file with the same
> name of the class, following this structure
>
> /Doctrine.php -> class Doctrine
> /Doctrine/Access.php -> class Doctrine_Access
> /Doctrine/Adapter.php -> class Doctrine_Adapter
> ...and so on.
You can do this with a combination of two things. First, add "Doctrine"
as an autoloader namespace, and second, add the Doctrine autoloader to
Zend_Loader_Autoloader.
The first is sipmly adding a line to your config file:
autoloaderNamespaces[] = "Doctrine"
Note the absence of a trailing '_' -- this is how it can match the
initial "Doctrine" class.
The second is pushing the Doctrine autoloader into
Zend_Loader_Autoloader during your Doctrine initialization. The
following is from an application bootstrap I have used successfully with
Doctrine already:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initDoctrine()
{
$this->getApplication()
->getAutoloader()
->pushAutoloader(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
}
}
Doing this, you no longer need to use the fallback autoloader
functionality.
> This is a recurring issue, since several framework and libraries have
> the same folder structure of Doctrine, is there a good practice to let
> the default autoloader manage them correctly?
PEAR is the library most problematic here, due to the fact that there is
no common vendor namespace. However, if you know that you are using
specific PEAR libraries, you can simply add their component namespaces
to the autoloader, as I did with the initial Doctrine setup, but using
the trailing '_'.
For libraries that *are* vendor namespaced, simply add their vendor
prefix to the autoloader namespaces, with the trailing '_'. This will
work with Solar, my own libraries (Phly_), etc.
--
Matthew Weier O'Phinney
Project Lead | matthew@zend.com
Zend Framework | http://framework.zend.com/
没有评论:
发表评论