阅读代码中的注释以获取说明:
<?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 World 中的引导文件不太理想的一个方面是,在我们使用它们之前,有很多 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() 混乱的问题,并确保只为任何给定请求加载所需的文件。