使用 PHP.scandir() 扫描文件,然后 require_once 它们
这是我迄今为止在 stackoverflow 上的第三个问题:
我正在定义文件及其在我的 first_run.php 文件上的位置, 我在这里定义的文件是那些包含类、辅助函数的文件 以及早期开发所需的任何其他文件
,这个first_run.php仅包含几行代码 但随着我添加一些新类或要包含的新文件
,并且由于我将文件的位置分组到特定文件夹内,因此该行逐渐增加,我想也许我可以扫描该文件夹,将检索到的文件的名称放入数组中然后循环 require_once ,这样我就不必每次在文件夹中添加新文件时都编辑first_run.php 。
我的第一个方法是使用 scandir()
before:
defined('INC_PATH') ? null : define('INC_PATH', SITE_ROOT.DS.'includes');
defined('MEMBERS') ? null : define('MEMBERS', INC_PATH.DS.'models'.DS.'members');
require_once(MEMBERS.DS.'member.php');
require_once(MEMBERS.DS.'phone.php');
require_once(MEMBERS.DS.'profile.php');
require_once(MEMBERS.DS.'talent.php');
require_once(MEMBERS.DS.'profile_picture.php');
require_once(MEMBERS.DS.'audio.php');
require_once(MEMBERS.DS.'video.php');
require_once(MEMBERS.DS.'gallery.php');
require_once(MEMBERS.DS.'statistik.php');
require_once(MEMBERS.DS.'inbox.php');
require_once(MEMBERS.DS.'comment.php');
require_once(MEMBERS.DS.'picked_stat.php');
require_once(MEMBERS.DS.'log.php');
after 是这样的:
$member_files = scandir(MEMBERS);
foreach($member_files as $member_file):
require_once(MEMBERS.DS.$member_file);
endforeach;
不过我还没有尝试“after”代码。 这可能吗?或者还有其他方法吗?或者我应该就这样保留它(继续添加行而不扫描文件)
提前致谢
this is my third question so far on stackoverflow :D
i am defining files and their location on my first_run.php files,
the files that i define here is those files containing classes, helper functions
and any other files required
at early development, this first_run.php contains only a few lines of codes
but the line is increasing gradually as i add some new classes or new files to be included
and since i group the file's location inside a particular folder, i figure that maybe i can scan the folder, put the name of the files retrieved into an array and then loop the require_once, so that i dont have to edit first_run.php every time i add a new file inside the folder.
my fisrt approach is using scandir()
before:
defined('INC_PATH') ? null : define('INC_PATH', SITE_ROOT.DS.'includes');
defined('MEMBERS') ? null : define('MEMBERS', INC_PATH.DS.'models'.DS.'members');
require_once(MEMBERS.DS.'member.php');
require_once(MEMBERS.DS.'phone.php');
require_once(MEMBERS.DS.'profile.php');
require_once(MEMBERS.DS.'talent.php');
require_once(MEMBERS.DS.'profile_picture.php');
require_once(MEMBERS.DS.'audio.php');
require_once(MEMBERS.DS.'video.php');
require_once(MEMBERS.DS.'gallery.php');
require_once(MEMBERS.DS.'statistik.php');
require_once(MEMBERS.DS.'inbox.php');
require_once(MEMBERS.DS.'comment.php');
require_once(MEMBERS.DS.'picked_stat.php');
require_once(MEMBERS.DS.'log.php');
after is something like:
$member_files = scandir(MEMBERS);
foreach($member_files as $member_file):
require_once(MEMBERS.DS.$member_file);
endforeach;
i havent try the 'after' code though.
is this possible?? or is there any other approach?? or should i just leave it that way (keep adding the lines without scanning the files)
thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
考虑使用自动加载。
通过自动加载,您根本不必费心包含文件。每当你实例化一个 PHP 不知道的新类时,PHP 将触发已注册的自动加载函数。该函数包含了所需的文件。这样,您只需在需要时加载所需的内容,这应该会提高性能。
PHP5.3 的简单示例
当您调用
new Foo
时,注册的自动加载函数将尝试包含/path/to/lib/and/Foo.php
中的类。建议使用类名约定,例如 fi PEAR,使查找文件变得更容易并减少 的数量include_paths
。为了提高安全性和速度,您可以提供一个更复杂的自动加载器,它使用数组从类名映射到文件名,从而确保只有实际属于应用程序一部分的文件才能包含在内。
进一步阅读:
Consider using Autoloading instead.
With autoloading, you do not have to bother with including files at all. Whenever you instantiate a new class that is not known to PHP at that point, PHP will trigger the registered autoload function. The function includes the required files then. This way, you only load what you need when you need it, which should increase performance.
Simple example with PHP5.3
When you call
new Foo
, the registered autoload function will try to include the class from/path/to/lib/and/Foo.php
. It is advisable to use a classname convention, like f.i. PEAR, to make finding files easier and to cut down on the amount ofinclude_paths
.For additional security and speed, you can provide a more sophisticated Autoloader that uses an array to map from classname to filename, thus making sure only files that actually are part of your application can get included.
Further reading:
这是可能的,但不推荐,就像如果有人可以在该目录上创建一个 php 文件,你最终会包含它,此外,你无法预测包含顺序。
试试这个:
如果您使用类,请考虑使用自动加载,正如 @Gordon 建议的那样。如果您没有使用类,请考虑使用它们:)
It's possible, but not recommended, like what if somebody could create a php file on that directory, you'll end up including it, besides, you can't predict the inclusion order.
Try this instead:
If you were using classes, consider using autoloading, as @Gordon suggested. And if you werent using classes, consider using them :)
乍一看你的代码可以工作,尽管你必须忽略“。”和 foreach 循环中的“..”。另外我会检查文件是否以“.php”结尾:
At a first glance your code could work, although you have to ignore "." and ".." in the foreach loop. Plus I'd check, if the file ends with ".php":
创建2个函数
create 2 functions