【问题标题】:Trouble setting up the Zend_Loader_Autoloader. Pretty easy question, but I'm new to Zend Framework设置 Zend_Loader_Autoloader 时遇到问题。很简单的问题,但我是 Zend 框架的新手
【发布时间】:2011-01-22 01:02:19
【问题描述】:

阅读代码中的 cmets 以获得说明:

<?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 中的引导文件 世界就是有很多 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() 混乱 并确保只有需要的文件 为任何给定的请求加载。

【问题讨论】:

    标签: php zend-framework autoload


    【解决方案1】:

    因为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

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多