[英]PHP array_walk require_once
I was just wondering if anyone knew why I can't use require_once
as a callback for array_walk
. 我只是想知道是否有人知道为什么我不能使用
require_once
作为array_walk
的回调。 I can just include it in an anonymous function and run that, but it gives an invalid callback error for the usage: 我可以将它包含在匿名函数中并运行它,但它为使用情况提供了无效的回调错误:
$includes = array(
'file1.php',
'file2.php',
'file3.php'
);
array_walk($includes, 'require_once');
require_once
不是PHP函数,而是控制结构。
You could create 你可以创造
function my_require_once ($name)
{
require_once $name;
}
The other guys are right, it's not a function. 其他人都是对的,这不是一个功能。 It operates outside the mode of the PHP code you write.
它在您编写的PHP代码模式之外运行。 The contents of the file are brought into the global namespace, even if it is called within a functiion, as above.
如上所述,即使在函数内调用,文件的内容也会被带入全局命名空间。
I use this, for example to do stuff like 我用它,例如做类似的东西
function my_log ($message, $extra_data = null)
{
global $php_library;
require_once "$php_library/log.php"; // big and complicated functions, so defer loading
my_log_fancy_stuff ($message, $extra_data);
}
You are going to waste more time finding out what's wrong. 你会浪费更多的时间来发现什么是错的。 Just use:
只需使用:
$includes = array(
'file1.php',
'file2.php',
'file3.php'
);
foreach($includes as $include) {
require_once($include);
}
As Martin wrote, require once is not a function, so your solution with array_walk is not been working. 正如Martin写的那样,require one不是一个函数,所以你的array_walk解决方案并没有起作用。 If you want to include multiple files, you can try to use this:
如果要包含多个文件,可以尝试使用:
function require_multi($files)
{
$files = func_get_args();
foreach($files as $file)
{
require_once($file);
}
}
Usage: 用法:
require_multi("fil1.php", "file2.php", "file3.php");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.