如何合并/消除 PHP 包含的类文件
我有多个文件,我单独保存。我想将它们全部放在一个文件中以减少文件 I/O,并且只包含一个文件。
更新
最终使用自动加载器的是 php.net
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我有多个文件,我单独保存。我想将它们全部放在一个文件中以减少文件 I/O,并且只包含一个文件。
更新
最终使用自动加载器的是 php.net
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(3)
PHP 将解析该文件。 PHP 可能会注册这些类,并且在任何情况下都会解析和语法检查该文件。但这可能仍然比加载单独的文件更快。更多单独的文件 I/O 会更慢,但由于只加载所需的文件,因此需要的内存更少。
我也不担心,因为仅类的内存消耗就接近于零。文件 I/O 可能也不会成为问题。您可能有 APC 缓存,它将 PHP 文件的编译版本保存到内存中,如果没有,您的操作系统可能会有一些缓存功能,这会导致经常使用的文件加载速度更快。
PHP will parse the file. PHP will probably register the classes, and will in any case parse and syntax check the file. But this might still be faster than loading separate files. More separate file I/O will be slower, though it will require less memory because only the needed files are loaded.
I wouldn't worry about either, because memory consuption for the classes alone is close to nil. File I/O will probably not be a problem either. You may have APC cache, which keeps compiled versions of the PHP files into memory, and if not, your OS will probably have some caching features which cause often used files to be loaded faster.
虽然没有什么可以阻止您将所有类对象合并到一个文件中,但这样做会违反基本的代码设计理念,并且最终会创建一个整体文件来管理。
如果您正在考虑为 require() 创建一个类,我建议将您的各个类保留为自己的文件,并使您的 consolidate.php 看起来更像这样:
至于 PHP 处理,调用 require() 或 include(文件上的 ) 实际上不会调用对象上的任何构造方法。也就是说,它确实包含整个文件。例如,包括session.php (2KB) 和general.php (3KB) 将仅包括5KB。根据您的代码库,包含所有内容可能会显着增加该大小。
While nothing is stopping you from consolidating all of your class objects into a singe file, to do so would be against basic code design philosophies and also eventually create a monolithic file to manage.
If you are thinking about making a single class to require(), I would suggest keeping your individual classes as their own files, and having your consolidate.php look more like this:
As for the PHP handling, calling require() or include() on a file does not actually call any constructing methods on objects. That said, it does include the entirety of the file. For example, including session.php (2KB) and general.php (3KB) would only include 5KB. Including everything could increase that size dramatically depending on your code base.
如果您正在开发所有类,则绝对不应该将它们放在一个文件中。这使得事情变得非常难以维护。每个类都有一个单独的文件,这意味着您确切地知道在哪里进行更改。是的,PHP会花时间解析您不使用的类。你为什么不想使用自动加载器?它们使事情变得更加容易,而且并不是很多开销或其他什么。
如果您担心 IO 效率,请缓存您的代码。
关于命名约定,我建议您在这里查看一个相当广泛接受的标准:
http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1
You should absolutely not put all your classes in one file if you're developing them. It makes things very difficult to maintain. A separate file for each class means you know exactly where to go to change something. And yes, PHP will spend time parsing classes you don't use. Why don't you want to use an autoloader? They make things so much easier, and it's not like they're a lot of overhead or anything.
If you're worried about IO efficiency, then cache your code.
Addressing naming conventions, I suggest you look at a pretty widely accepted standard here:
http://groups.google.com/group/php-standards/web/psr-0-final-proposal?pli=1