像在基于文件的Session中那样在PHP中包含很多文件是否不好? [英] Is it bad to include a lot of files in PHP like it is for file based Sessions?

查看:110
本文介绍了像在基于文件的Session中那样在PHP中包含很多文件是否不好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了基于文件的PHP会话如何不是最大的性能后,我想到了.这是否意味着包含很多文件的PHP脚本也是不好的?因为它包含一个文件,或者这与检索会话数据文件的方式不同?

After reading about how file based PHP sessions are not the greatest for performance, it has me thinking. Does this mean a PHP script including a lot of files is bad as well? Since it is including a file or is this different from the way session data files are retrieved?

推荐答案

您应该使用spl_autoload_register()和OOP.这样,无论您的项目当前有多小,或者随着时间的推移它会发展到多大(排除这种可能性都是愚蠢的),PHP只会包含它所需要的内容,不会多也不会少.

You should use spl_autoload_register() and OOP. This way, no matter how small your project currently is or how big it will evolve over time (and it would be dumb to exclude this possibility), PHP will only include what it needs, no more, no less.

这是运行时RAM使用率,代码的可维护性和硬盘延迟时间的影响之间的完美的面向未来平衡,我想说的是,只要您正确地对代码进行模块化,当然(( XDebug 可以在这里提供帮助).

That's the perfect future-oriented balance between runtime RAM usage, the maintainability of the code and the effects of hard disk latency time, I'd say, provided you're modularizing your code properly, of course (and XDebug helps here).

话虽如此,这意味着包含未使用的文件的弊端.

Having said that, it implies the badness of including unused files.

由于php.ini指令include_path,无论采用哪种方式(无论是spl_autoload_register()还是其他方式),都应该使用绝对路径来包含文件.在使用相对路径时,PHP会通过该路径搜索文件.

Inclusion of files, no matter which way (spl_autoload_register() or otherwise), should be done with absolute paths, due to the php.ini directive include_path, which PHP would search through for your files when using relative paths.

还有一个小小的注释,说明为什么"include'foo.php'"的工作方式类似于"include'./foo.php'"(包括文件的正常"方式):这是因为目录."默认情况下是include_path的一部分.

And a small extra-note to why "include 'foo.php'" works like "include './foo.php'" (the "normal" way of including files): it's because the directory "." is part of include_path by default.

这篇关于像在基于文件的Session中那样在PHP中包含很多文件是否不好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆