Is it possible in the current stable version of the Zend Framework (1.11), to work with application classes using PHP namespaces?
在当前稳定版本的Zend Framework(1.11)中,是否可以使用PHP命名空间处理应用程序类?
Application\Form\Abc instead of Application_Form_Abc
Application\Model\Xyz instead of Application_Model_Xyz
etc.
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.
从v1.10开始,ZF支持自动加载命名空间,并且在包含命名空间库时它工作正常,但是在尝试使用应用程序类执行相同的工作时我没有成功。
2 个解决方案
#1
7
Actually there is a simple workaround suggested by Dmitry on the ZF issue tracker:
实际上,Dmitry在ZF问题跟踪器上提出了一个简单的解决方法:
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!
对我来说就像一个魅力!
#2
5
The standard autloader introduced in 1.12 allows you to use namespaces with minimal effort:
1.12中引入的标准自动加载器允许您以最小的努力使用命名空间:
require 'Zend/Loader/AutoloaderFactory.php';
Zend_Loader_AutoloaderFactory::factory([
'Zend_Loader_StandardAutoloader' => [
'autoregister_zf' => true,
'namespaces' => [
'Application' => '/path/to/Application/src',
]
]
]);
Now you can use Application\Form\Abc
instead of Application_Form_Abc
& Application\Model\Xyz
instead of Application_Model_Xyz
etc.
现在您可以使用Application \ Form \ Abc而不是Application_Form_Abc和Application \ Model \ Xyz而不是Application_Model_Xyz等。
Directory/File structure examples:
目录/文件结构示例:
path/to/Application/src/Form/Abc.php
<?php
namespace Application/Form;
class Abc {}
path/to/Application/src/Model/Xyz.php
<?php
namespace Application/Model;
class Xyz {}
#1
7
Actually there is a simple workaround suggested by Dmitry on the ZF issue tracker:
实际上,Dmitry在ZF问题跟踪器上提出了一个简单的解决方法:
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!
对我来说就像一个魅力!
#2
5
The standard autloader introduced in 1.12 allows you to use namespaces with minimal effort:
1.12中引入的标准自动加载器允许您以最小的努力使用命名空间:
require 'Zend/Loader/AutoloaderFactory.php';
Zend_Loader_AutoloaderFactory::factory([
'Zend_Loader_StandardAutoloader' => [
'autoregister_zf' => true,
'namespaces' => [
'Application' => '/path/to/Application/src',
]
]
]);
Now you can use Application\Form\Abc
instead of Application_Form_Abc
& Application\Model\Xyz
instead of Application_Model_Xyz
etc.
现在您可以使用Application \ Form \ Abc而不是Application_Form_Abc和Application \ Model \ Xyz而不是Application_Model_Xyz等。
Directory/File structure examples:
目录/文件结构示例:
path/to/Application/src/Form/Abc.php
<?php
namespace Application/Form;
class Abc {}
path/to/Application/src/Model/Xyz.php
<?php
namespace Application/Model;
class Xyz {}