惰性类包括 PHP
我使用自动加载器来包含类。我现在正在做的是使用“glob”读取不同的目录并将它们推入数组中。对此有更好的解决方案吗?
$path = './';
$files = array_merge(
glob($path.'includes/classes/system/*.class.php'),
glob($path.'includes/classes/system/baseclasses/*.class.php'),
glob($path.'includes/classes/system/systementities/*.class.php'));
编辑:
我在自动加载类中使用它。问题是我必须用 glob 搜索文件。有没有更快的方法来做到这一点?
I use a autoloader to include classes. What i'm doing right now is using "glob" to read different dir's and push them in an array. Is there a better solution to this?
$path = './';
$files = array_merge(
glob($path.'includes/classes/system/*.class.php'),
glob($path.'includes/classes/system/baseclasses/*.class.php'),
glob($path.'includes/classes/system/systementities/*.class.php'));
EDIT:
I use this inside an autoload class. The problem is that I have to search for the files with glob. Is there a faster way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每当您尝试使用 PHP 不认识的类时,就会触发自动加载。如果您使用
include
/require
,PHP 将遍历您在 PHP.ini 中为include_path
指定的所有目录,因此应该没有理由使用glob
。在您的情况下,将include_path
设置为就足够了。包含文件的最快方法是使用 class2file 映射。但是,这要求您创建这样的地图并在修改应用程序时保持更新。
Autoloading will be triggered whenever you are trying to use a class that isn't know to PHP. If you use
include
/require
, PHP will go through all the directories you specified for yourinclude_path
in PHP.ini, so there should no reason to useglob
. In your case, it should be sufficient to set theinclude_path
toThe fastest way to include files would be to use a class2file map. However, this requires you to create such a map and keep it updated when you modify your application.
PHP 已经为您解决了这个问题。使用 PHP 的内置自动加载器。当语言/框架为您提供时,切勿自行开发。
PHP has already solved this problem for you. Use PHP's built-in autoloader. Never roll your own when the language/framework provides it for you.
...这仍然不是最好的方法,但它可以帮助您使用 glob()。如果您通常加载所有系统实体,则引导/配置脚本中应该有一个硬编码的 require() 列表。关于如何组织和获取类文件有很多选项。为了减少搜索脚本的时间,您可以将文件树视为一个接口。为了简单起见,您可以将所有非强制类放在同一个文件夹中,从而将 is_file() 调用减少到一次。
在我的系统中,我将对象类分组在具有各自控制脚本和视图的文件夹中。我的 __autoload() 函数有一个更简单的工作——
...which is still not the greatest way, but it gets you around using glob(). If you are commonly loading all of your systementities there should be a hard-coded require() list in a boot/config script. There are a lot of options on how to organize and fetch class files. To reduce the time searching for scripts you could think of your filetree as an interface. For the sake of simplicity, you could put all non-mandatory classes in the same folder, reducing the is_file() calls down to one.
In my system I have object classes grouped in folders with their respective control scripts and views. My __autoload() function has a simpler job --