PHP 导入包还是自动加载?
您建议使用什么解决方案将文件包含在 PHP 项目中?
- 不需要手动调用 require/include 函数 - 所有内容都通过自动加载函数
- 在需要时导入包来加载。
下面是包导入 API:
import('util.html.HTMLParser');
import('template.arras.*');
在此函数声明中,您可以用点(包层次分隔符)分解字符串,循环遍历特定包(文件夹)中的文件,以仅包含其中一个或全部(如果在以下位置找到星号符号)字符串的末尾,例如('template.arras.*')。
我在包导入方法中看到的好处之一是,它可以迫使您使用更好的对象分解和类分组。
我在自动加载方法中看到的缺点之一是自动加载函数可能变得非常大并且不是很明显/可读。
你怎么看待这件事?
- 您能说出每种方法的优点/缺点是什么?
- 我怎样才能找到适合该项目的最佳解决方案?
- 如何知道使用包管理是否会出现性能问题?
What solution would you recommend for including files in a PHP project?
- There aren't manual calls of require/include functions - everything loads through autoload functions
- Package importing, when needed.
Here is the package importing API:
import('util.html.HTMLParser');
import('template.arras.*');
In this function declaration you can explode the string with dots (package hierarchy delimeter), looping through files in particular package (folder) to include just one of them or all of them if the asterisk symbol is found at the end of the string, e.g. ('template.arras.*').
One of the benefits I can see in package importing method, is that it can force you to use better object decomposition and class grouping.
One of the drawbacks I can see in autoload method - is that autoload function can become very big and not very obvious/readable.
What do you think about it?
- What benefits/drawbacks can you name in each of this methods?
- How can I find the best solution for the project?
- How can I know if there will be any performance problems if package management is used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我广泛使用 __autoload() 。 我们在应用程序中使用的 autload 函数对旧类的向后兼容性进行了一些调整,但在创建允许 autoload() 顺利工作的新类时,我们通常遵循一个约定:
I use __autoload() extensively. The autload function that we use in our application has a few tweaks for backwards compatibility of older classes, but we generally follow a convention when creating new classes that allow the autoload() to work fairly seemlessly:
导入方法是一种改进,但仍然加载超出需要的内容。
通过使用星号或在脚本开头加载它们(因为在每个“新类名”之前导入会变得很麻烦)
我是
__autoload()
甚至更好的spl_autoload_register()
因为它只包括您正在使用的类以及不关心类所在位置的额外好处。 如果您的大学将文件移动到另一个目录,您不会受到影响。
缺点是需要额外的逻辑
使其与目录正常工作。
The import method is an improvement but still loads up more than needed.
Either by using the asterisk or loading them up in the beginning of the script (because importing before every "new Classname" will become cumbersome)
I'm a fan of
__autoload()
or the even betterspl_autoload_register()
Because it will include only the classes you're using and the extra benefit of not caring where the class is located. If your colleges moves a file to another directory you are not effected.
The downside is that it need additional logic
to make it work properly with directories.
我使用 require_once("../path-to-auto-load-script.php.inc") 进行自动加载
我对所有类和 inc 文件都有一个标准命名约定,这使得以编程方式确定当前的类名变得更容易被要求。
例如,所有类都有一个特定的扩展名,如 inc.php
(所以我知道它们将位于 /cls 目录中)
和
所有 inc 文件都以 .ht 开头(因此它们将位于 /inc 目录中)
自动加载接受一个参数:className,然后我用它来确定文件的实际位置。 一旦我知道我的目标目录是什么,就会循环,每次添加“../”来解释子子页面(这似乎破坏了我的自动加载),最后一旦找到实际的代码文件就需要_once。
I use require_once("../path-to-auto-load-script.php.inc") with auto load
I have a standard naming convention for all classes and inc files which makes it easier to programaticaly determine what class name is currently being requested.
for example, all classes have a certain extension like inc.php
(so I know that they'll be in the /cls directory)
and
all inc files start with .ht (so they'll be in the /inc directory)
auto load accepts one parameter: className, which I then use to determine where the file is actually located. looping once I know what my target directory is, each time adding "../" to account for sub sub pages, (which seemed to break auto load for me) and finally require_once'ing the actual code file once found.
我强烈建议执行以下操作:
将所有类放入静态数组中,className => 文件路径/类文件。 自动加载功能可以使用它来加载类。
这可确保您始终加载最少数量的文件。 这也意味着您可以避免完全愚蠢的类名以及对所述名称的解析。
如果它很慢,您可以使用一些加速器,这将为您带来更多好处,如果它仍然很慢,您可以通过“编译”过程来运行,其中经常使用的文件只是转储到通用文件中,并且可以更新自动加载引用以指向正确的位置。
如果您开始遇到自动加载太慢的问题,我觉得很难相信,您可以根据包将其拆分,并具有多个自动加载函数,这样只需要数组的子集,如果您的软件包是围绕软件模块定义的(登录、管理、电子邮件等)
I strongly suggest doing the following instead:
Throw all your classes into a static array, className => filepath/classFile. The auto load function can use that to load classes.
This ensures that you always load the minimum amount of files. This also means you avoid completely silly class names, and parsing of said names.
If it's slow, you can throw on some accelerator, and that will gain you a whole lot more, if it still is slow, you can run things through a 'compile' process, where often used files are just dumped into common files, and the autoload references can be updated to point to the correct place.
If you start running into issues where your autoloading is too slow, which I find hard to believe, you can split that up according to packages, and have multiple autoloading functions, this way only subsets of the array are required, this works best if your packages are defined around modules of your software (login, admin, email, ...)
我不喜欢
__autoload()
。 在许多库(例如某些 PEAR 库)中,开发人员使用class_exists()
而不传递相对较新的第二个参数。 您拥有的任何旧代码也可能存在此问题。 如果您定义了__autoload()
,这可能会导致警告和错误。如果您的库很清晰,并且您没有遗留代码需要处理,那么它是一个很棒的工具。 有时我希望 PHP 在管理
class_exists()
行为方面能够更聪明一些,因为我认为问题在于该功能而不是__autoload()
。I'm not a fan of
__autoload()
. In a lot of libraries (some PEAR libraries, for instance), developersuseclass_exists()
without passing in the relatively new second parameter. Any legacy code you have could also have this issue. This can cause warnings and errors if you have an__autoload()
defined.If your libraries are clear though, and you don't have legacy code to deal with, it's a fantastic tool. I sometimes wish PHP had been a little smarter about how they managed the behavior of
class_exists()
, because I think the problem is with that functionality rather than__autoload()
.滚动自己的包装系统可能是一个坏主意。 我建议您使用明确的手动包含或自动加载(或与此相关的组合)。
Rolling your own packaging system is probably a bad idea. I would suggest that you go with explicit manual includes, or with autoload (or a combination for that matter).