I've tried to include the contents from a list of files:
$files = [
'a.php',
'b.php',
];
$contents = array_map('require', $files);
But this didn't work. The error I get is:
Warning: array_map() expects parameter 1 to be a valid callback, function 'require' not found or invalid function name
Why is this and is there a way to make it work?
Because require isn't a function, it's a language construct.
You'll need to create a valid callback function to do the actual require, or use an autoloader
Agree with Mark Baker as it's not function , You can use _autoload() in case you are including class files.
As everyone already noticed - it is a language construct.
You can try this
$includes = array('a.php', 'b.php');
array_map(function($file){
require $file;
}, $includes);
use autoload or spl_autoload_register
to comfort work use namespaces
or you may use array_wallk without return value and anonunys function
`$files` = [
'a.php',
'b.php',
];
`$contents` = array_map(`$files`, function(`$el`){
require(`$el`);
});
//but foreach working faster:
foreach(`$files` as `$item`){
require(`$item`
);}`