为什么 Zend Bootstrap 自动加载功能中有这两行

发布于 2024-10-06 22:02:47 字数 462 浏览 0 评论 0原文

我见过有这个代码块的例子和其他没有注释两行的例子,也许没有。这两行代码的具体用途是什么?

$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 技术交流群。

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

发布评论

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

评论(4

灰色世界里的红玫瑰 2024-10-13 22:02:47

第二行是一种解决方法,适用于标准结构自动加载器不够的情况,应该避免,因为它会减慢速度(即使有时它非常方便)。

看起来后备自动装载机在 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.

吹泡泡o 2024-10-13 22:02:47

它将导致指定的自动加载器用于任何未命名空间的命名空间和类,如果没有定义该命名空间的自动加载器,则有效地忽略“命名空间”参数。

当您有不属于某个命名空间的类,或者您需要跨多个不同的命名空间使用类时,适合使用 $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.

娇纵 2024-10-13 22:02:47

第一行很简单,第二行在手册中提到:这意味着 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.

与风相奔跑 2024-10-13 22:02:47

首先是获取自动加载器的实例,就是这样。由于自动加载器是单例,因此它只能有一个实例。如果该实例尚不存在,则会创建它。

第二行是让自动加载器充当包罗万象的角色 - 每当未找到某些内容时,自动加载器就会被触发。

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.

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