PHP return 来自匿名文件的数据

PHP return data from anonymous in file

我看到了一个看起来像这样的文件:

my-file.php

<?php
return function($some, $args) {
  return array(
    'foo' => 'my first template variable',
    'bar' => 'my second template variable'
  );
};

这是一个匿名函数,return 数据以某种方式获取并在其他地方使用。

如何从这样的文件中获取数据?

我一直在搜索和调查其他问题,但没有找到类似的问题。

所以相关文档可以在include document and the anonymous function document.

上找到

总而言之,包含/必需的文件可以 return 值。然后可以将这些分配给变量。

在这种情况下,您可以执行以下操作:

$callable = require 'my-file.php';

这会将匿名函数分配给 $callable,此时您可以将其视为标准闭包(因为它就是这样)并像调用函数一样调用它:

$callable($anyargs, $needed);

在这种情况下,return 数组中的数据。