在 PHP 中使用自动加载有什么不好吗?

发布于 2024-08-04 00:17:47 字数 729 浏览 5 评论 0 原文

来自 php.net

在 PHP 5 中,这不再是必需的。您可以定义一个 __autoload()如果您尝试使用尚未定义的类/接口,则会自动调用该函数。通过调用此函数,脚本引擎将在 PHP 因错误而失败之前获得最后一次加载类的机会。

现在我想知道,仅使用 __autoload 在动态站点上加载适当的类是一种不好的做法吗?

我的网站设置方式是将文件包含到 index.php 文件中,例如 http://www.site.com/index.php?p=PAGE-I-WANT-TO-LOAD

所以如果我在论坛上部分或我网站的博客部分,我只想加载适当的类和函数,因此我使用自动加载,但我从不手动包含文件,我应该使用 __autoload 作为最后的手段还是即使在高流量系统上我也做得很好?

From php.net:

In PHP 5, this is no longer necessary. You may define an __autoload() function which is automatically called in case you are trying to use a class/interface which hasn't been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

Now I am wanting to know, is it bad practice to solely use __autoload to load the appropriate classes on a dynamic site?

The way my site is setup is to include files into the index.php file, for example http://www.site.com/index.php?p=PAGE-I-WANT-TO-LOAD

So if I am on the forums section or the blogs section of my site, I want only appropriate classes and functions to be loaded, so I use autoload but I never include a file manually, should I be using __autoload as a last resort or is what I am doing fine even on a high traffic system?

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

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

发布评论

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

评论(4

计㈡愣 2024-08-11 00:17:47

坏的?不。__autoload() 是我最喜欢的 PHP 5 新增功能之一。它消除了手动包含/要求应用程序所需的类文件的责任(和烦恼)。话虽如此,作为开发人员,您有责任确保仅加载“适当的类”。通过结构化命名方案和目录结构可以轻松完成此操作。网上有很多关于如何正确使用 __autoload() 的示例,进行 Google 搜索,您会发现大量信息。

Bad? No. __autoload() is one of my favorite additions to PHP 5. It removes the responsibility (and annoyance) of manually having to include/require the class files necessary to your application. That being said, it's up to you as the developer to ensure that only the 'appropriate classes' are loaded. This is easily done with a structured naming scheme and directory structure. There are plenty examples online of how to properly use __autoload(), do a Google search and you'll find plenty of information.

南汐寒笙箫 2024-08-11 00:17:47

自动加载是只加载需要的类的好方法。

在 PHP 5 >= 5.1.2 中,旧的 __autoload() 的大部分问题都消失了,这要归功于 spl_autoload_register()

Autoload is a good way to load only what classes is needed.

In PHP 5 >= 5.1.2, most of the problems with the old __autoload() dissapeared, thanks to spl_autoload_register().

独自唱情﹋歌 2024-08-11 00:17:47

现在我想知道,单独使用 __autoload 在动态站点上加载适当的类是不是不好的做法?

一点也不。您可以依赖自动加载,您所需要做的就是设计一个良好的命名约定并实现一个高效的自动加载器。

有一个主要问题需要考虑。自动加载和 Zend Guard 不能很好地协同工作,因为 Zend Guard 倾向于重命名事物,这意味着您决定使用的命名约定很可能不相同。如果您将使用 Zend Guard(或任何其他混淆器),您很可能被迫手动包含所有文件。

以下是 Zend Guard 用户指南中的引用:

自动加载类将不起作用,因为磁盘上的文件名不可用
匹配混淆的类名。

Now I am wanting to know, is it bad practice to solely use __autoload to load the appropriate classes on a dynamic site?

Not at all. You can rely on autoload, all you need to do is to devise a good naming convention and implement an efficient autoloader.

There is one major issue to consider. Autoloading and Zend Guard do not play well together, because Zend Guard tends to rename things, which will mean that the naming convention you decided to use will most likely not be the same. If you will be using Zend Guard (or any other obfuscator for that matter) you will most likely be forced to include all the files by hand.

Here is a quote from the Zend Guard user guide:

Autoloading classes will not work since the filename on the disk would not
match the obfuscated class name.

羁〃客ぐ 2024-08-11 00:17:47

__autoload() 的唯一危险是如果您定义了一个糟糕的自动加载函数。一般来说,当 PHP 寻找包含您的类的正确文件时,您所获得的性能损失只是几次磁盘搜索。好处是摆脱所有那些烦人的 include() 调用。

如果您担心此级别的性能,那么您应该已经在使用操作码缓存,例如 APC。

The only danger to __autoload() is if you define a poor autoloading function. Generally, all you're going to get in terms of a performance hit is a few disk seeks as PHP looks for the right files that contain your classes. The upside is getting rid of all those annoying include() calls.

If you're worried about performance at this level, then you should already be using an opcode cache such as APC.

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