阅读代码中的注释以获取描述:
<?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();
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试按照一个教程,要求我配置我的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()杂乱的问题,并确保只为任何给定的请求加载所需的文件.
因为在ZF1.8中更改了自动加载,所以应该替换
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload();
Run Code Online (Sandbox Code Playgroud)
同
require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('App_');
Run Code Online (Sandbox Code Playgroud)
或使用后备自动加载器
$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);
Run Code Online (Sandbox Code Playgroud)
根据教程的年龄,我建议在Rob Allen的博客上查看最近的ZF1.10教程.
| 归档时间: |
|
| 查看次数: |
1546 次 |
| 最近记录: |