为什么 Zend Bootstrap 自动加载功能中有这两行
我见过有这个代码块的例子和其他没有注释两行的例子,也许没有。这两行代码的具体用途是什么?
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH)
);
$autoloader = Zend_Loader_Autoloader::getInstance(); //MAYBE NOT
$autoloader->setFallbackAutoloader(true); //MAYBE NOT
return $moduleLoader;
额外:
给定答案,它是 这样做是好事吗?有没有 开销或任何不好的事情 关于它,或者这是标准 练习?
I've seen examples that has this block of code and other examples that didn't have the 2 lines commented MAYBE NOT. What exactly is the purpose of these 2 lines?
$moduleLoader = new Zend_Application_Module_Autoloader(array(
'namespace' => '',
'basePath' => APPLICATION_PATH)
);
$autoloader = Zend_Loader_Autoloader::getInstance(); //MAYBE NOT
$autoloader->setFallbackAutoloader(true); //MAYBE NOT
return $moduleLoader;
Added:
given the answers, is it a
good thing to do it this way? Is there
overhead or anything that's not good
about it, or is this the standard
practice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第二行是一种解决方法,适用于标准结构自动加载器不够的情况,应该避免,因为它会减慢速度(即使有时它非常方便)。
看起来后备自动装载机在 ZF 2.0 中将不可用。
The second line is kind of workaround for cases when standard structure autoloader is not enough, and should be avoided, because it slows things down (even if sometimes it is very handy).
Looks like the fallback autoloader will not be available in ZF 2.0.
它将导致指定的自动加载器用于任何未命名空间的命名空间和类,如果没有定义该命名空间的自动加载器,则有效地忽略“命名空间”参数。
当您有不属于某个命名空间的类,或者您需要跨多个不同的命名空间使用类时,适合使用 $autoloader->setFallbackAutoloader(true) 。
It will cause the specified autoloader to be used for any namespace and classes that are not namespaced, effectively ignoring 'namespace' parameter if no autoloader for that namespace is defined.
It is appropriate to use
$autoloader->setFallbackAutoloader(true)
when you have classes that do not belong to a namespace, or you need to use classes across many different namespaces.第一行很简单,第二行在手册中提到:这意味着 Zend Autoloader 将尝试(自动)加载每个类,而不仅仅是作为前缀提到的类或注册的 Autoloader。
First line is trivial, second line is mentioned at the manual: It means, that the Zend Autoloader will try to (auto)load every class, not only the classes mentioned as prefix, or registered Autoloader.
首先是获取自动加载器的实例,就是这样。由于自动加载器是单例,因此它只能有一个实例。如果该实例尚不存在,则会创建它。
第二行是让自动加载器充当包罗万象的角色 - 每当未找到某些内容时,自动加载器就会被触发。
The first is to get the instance of the autoloader, that is it. As the autoloader is a singleton it can only have one instance. If that instance doesn't exist yet, it will be created.
The second line is to have the autoloader act as a catch all - whenever something isn't found, the autoloader is triggered.