无法设置Zend_Loader_Autoloader。很简单的问题,但我是Zend Framework的新手

时间:2010-02-28 16:37:24

标签: php zend-framework autoload

阅读代码中的注释以获取描述:

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function __construct($configSection){
        $rootDir = dirname(dirname(__FILE__));
        define('ROOT_DIR',$rootDir);

        set_include_path(get_include_path()
        . PATH_SEPARATOR . ROOT_DIR . '/library/'
        . PATH_SEPARATOR . ROOT_DIR .
        'application/models'
        );

        //PROBLEM LIES HERE, BEWARE OF DRAGONS.
        //Using this, I receive a deprecated warning.
        include 'Zend/Loader.php';
        Zend_Loader::registerAutoload();        

        //Using this, I recieve an error that autoload() has missing arguments.     
        //Zend_Loader_Autoloader::autoload();       

        //Load the configuration file.
        Zend_Registry::set('configSection', $configSection);
        $config = new Zend_Config_Ini(ROOT_DIR . '/application/config.ini',$configSection);

        Zend_Registry::set('config',$config);
        date_default_timezone_set($config->date_default_timezone);

        //Database configuration settings go here. :)
        $db = Zend_Db::factory($config->db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('db',$db);
    }

    public function configureFrontController(){
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setControllerDirectory(ROOT_DIR . '/application/controllers');
    }

    public function runApp(){
        $this->configureFrontController();

        //Runs the Zend application. :)
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->dispath();
    }
}

我正在尝试按照一个教程,要求我配置我的Zend应用程序以使用它提供的自动加载功能。

当使用registerAutoLoad()方法时,我收到一个弃用的警告,它告诉我在我的代码中使用另一个方法,即它下面的方法。

我该怎么办?

编辑:为什么我使用弃用的方法:

  

一个不太理想的方面   原始Hello中的bootstrap文件   世界是有很多的   Zend_Loader :: loadClass()调用加载   在我们使用之前我们需要的课程   他们。

     

在较大的应用程序中,甚至有   更多正在使用的课程,导致   整个应用程序杂乱无章   只是为了确保合适的班级   包括在合适的时间。

     

对于我们的Places网站,我们使用PHP   __autoload()功能使PHP将自动加载我们的类   为了我们。 PHP5介绍了   每次尝试实例化时都会调用__autoload()魔术函数   一个尚未定义的类。

     

Zend_Loader类有一个特殊的   具体就是registerAutoload()方法   与__autoload()一起使用,如图所示   清单3.1 b。这种方法会   自动使用PHP5的标准PHP   库(SPL)spl_autoload_register()   功能使多个自动加载器   可以使用。

     

Zend_Loader :: registerAutoload()之后   每当一堂课时,都会被召唤   实例化但尚未实现   已定义,包含该类的文件   已经包括了。这解决了这个问题   Zend_Loader :: loadClass()杂乱   并确保只有所需的文件   为任何给定的请求加载。

1 个答案:

答案 0 :(得分:5)

因为Autoloading was changed in ZF1.8你应该替换

require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('App_');

或使用带有

的后备自动加载器
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

根据教程的年龄,我建议您同时查看tutorial for the recent ZF1.10 at Rob Allen's blog