扫描目录中的php文件,并将它们包含在当前脚本中
Would it be a good idea to include extensions for a script this way?
for eg. using glob to get a list of php files from a certain directory, and do a require_once
for each file.
this would run each time the page is generated. would it be bad for performance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这会很糟糕,原因有两个:
evil.php
粘贴到您的目录中,它可能会被包含并执行。glob
不是最有效的,也不是通过相对路径包含的。也许可以考虑使用自动加载。
It would be bad for two reasons:
evil.php
in your directory it could be included and executed.glob
is not the most efficient, nor is including via a relative path.Perhaps look into using autoloading.
您可以考虑使用 __autoload() 代替。
You might consider using __autoload() instead.
这不是特别好的做法:无论您是否需要它们,您都会包含文件。您也无法控制包含/要求处理文件之间的任何依赖关系的顺序。如果这些是 PHP 类文件,那么使用自动加载器将是更好的选择。
It isn't particularly good practise: you're including files irrespective of whether you need them or not. Nor can you control the order of including/requiring to handle any dependencies between the files. If these are PHP class files, then using an autoloader would be a better option.