在Zend Framework(v1)应用程序中使用PHP名称空间 [英] Using PHP namespaces in a Zend Framework (v1) application

查看:69
本文介绍了在Zend Framework(v1)应用程序中使用PHP名称空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在当前稳定的Zend Framework(1.11)版本中,是否可以使用PHP名称空间处理应用程序类?

Is it possible in the current stable version of the Zend Framework (1.11), to work with application classes using PHP namespaces?

Application\Form\Abc instead of Application_Form_Abc
Application\Model\Xyz instead of Application_Model_Xyz
etc.

从v1.10开始,ZF支持自动加载名称空间 ,并且在包含命名空间的库时工作正常,但是尝试对应用程序类执行相同操作时我没有成功.

Starting from v1.10, ZF supports autoloading namespaces, and it's working fine when including namespaced libraries, but I was unsuccessful when trying to do the same job with application classes.

推荐答案

实际上,有一个简单的解决方法由德米特里(Dmitry)提出的关于采埃孚(ZF)问题跟踪器的建议:

Actually there is a simple workaround suggested by Dmitry on the ZF issue tracker:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initAutoloader()
    {
        $loader = function($className) {
            $className = str_replace('\\', '_', $className);
            Zend_Loader_Autoloader::autoload($className);
        };

        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->pushAutoloader($loader, 'Application\\');
    }
}

对我来说像是一种魅力!

Works like a charm for me!

这篇关于在Zend Framework(v1)应用程序中使用PHP名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆