设置 Zend_Loader_Autoloader 时遇到问题。很简单的问题,但我是 Zend Framework 的新手

发布于 2024-08-22 21:40:41 字数 2574 浏览 1 评论 0原文

阅读代码中的注释以获取说明:

<?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这个方法将 自动使用 PHP5 的标准 PHP 库 (SPL) spl_autoload_register() 功能,以便多个自动加载器 可以使用。

在 Zend_Loader::registerAutoload() 之后 每当有一个类被调用时 尚未实例化 定义,包含类的文件 包括在内。这解决了问题 Zend_Loader::loadClass() 的混乱 并确保只有需要的文件 针对任何给定的请求加载。

Read the comments in the code for a description:

<?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();
    }
}

I'm trying to follow a tutorial that wants me to configure my Zend app to use the autoloading functions that it offers.

When using the registerAutoLoad() method, I receive a deprecated warning and it tells me to use another method, the one below it in my code.

What can I do?

Edit: Why I was using the deprecated method:

One less than ideal aspect of the
bootstrap file in the original Hello
World is that there are a lot of
Zend_Loader::loadClass() calls to load
up the classes we need before we use
them.

In larger applications, there are even
more classes in use, resulting in
clutter throughout the application
just to ensure that the right classes
are included at the right time.

For our Places website, we use PHP’s
__autoload() functionality so that PHP will automatically load our classes
for us. PHP5 introduced the
__autoload() magic function that is called whenever you try to instantiate
a class that hasn’t yet been defined.

The Zend_Loader class has a special
registerAutoload() method specifically
for use with __autoload(), as shown in
listing 3.1 b. This method will
automatically use PHP5’s Standard PHP
Library (SPL) spl_autoload_register()
function so that multiple autoloaders
can be used.

After Zend_Loader::registerAutoload()
has been called, whenever a class is
instantiated that has not yet been
defined, the file containing the class
is included. This solves the problem
of Zend_Loader::loadClass() clutter
and ensures that only the needed files
are loaded for any given request.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只怪假的太真实 2024-08-29 21:40:41

因为 自动加载在 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);

取决于您的教程的年龄,我建议查看 Rob Allen 博客上最近的 ZF1.10 教程

Because Autoloading was changed in ZF1.8 you should replace

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

with

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

or use the fallback autoloader with

$loader->setFallbackAutoloader(true);
$loader->suppressNotFoundWarnings(false);

Depending on the age of your tutorial, I suggest checking out the tutorial for the recent ZF1.10 at Rob Allen's blog as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文