Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use 'require' with 'array_map'?

Tags:

php

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?

like image 270
Michael Härtl Avatar asked May 07 '26 19:05

Michael Härtl


2 Answers

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

like image 104
Mark Baker Avatar answered May 09 '26 08:05

Mark Baker


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);
like image 22
Aleksander Wons Avatar answered May 09 '26 08:05

Aleksander Wons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!