将函数存储为数组php的元素

时间:2021-09-25 15:40:18

Is it possible store a few functions in an array by reference or by anonymous function?

是否可以通过引用或匿名函数在数组中存储一些函数?

For example:

例如:

 $array = [fun1, function(){ /*do something*/ }, fun3];

where fun1 and fun3 are defined as

其中fun1和fun3定义为

 function fun1(){/*do something*/}

3 个解决方案

#1


4  

As long as your PHP version is >= 5.3 you'll be able to harness anonymous functions and regular functions in your array:

只要您的PHP版本> = 5.3,您就可以在数组中使用匿名函数和常规函数:

function yourFunction($string){
    echo $string . " : by reference";
};

$array = array(
    'a' => function($string){
        echo $string;
    },
    'b' => 'yourFunction',
);

You can use the call_user_func or call_user_func_array functions.

您可以使用call_user_func或call_user_func_array函数。

call_user_func($array['a'], 'I love things');
call_user_func($array['b'], 'I love things');

Or As @Andrew stated in the comments too, you could call it as the following:

或者正如@Andrew在评论中所述,您可以将其称为以下内容:

$array['a']('I love things');
$array['b']('I love things');

If you'd like to read more about these callback methods, it's filed under the callback pseudo-type documentation on PHP.net and it is well worth a read!

如果您想了解有关这些回调方法的更多信息,请在PHP.net上的回调伪类型文档下提交,这非常值得一读!

#2


2  

Yes, you can do that, however you can't store the function as literal, but instead you have to refer to it by its name:

是的,您可以这样做,但是您不能将该函数存储为文字,而是您必须通过其名称来引用它:

$array = ['fun1', function () { /*do something*/ }, 'fun3'];

foreach ($array as $fun) {
    $fun();
}

Note that because it's by name, you'll have to use the fully qualified name should your function happen to be in a namespace; e.g. 'foo\bar\baz'.

请注意,因为它是按名称,如果您的函数恰好位于命名空间中,则必须使用完全限定名称;例如“富\酒吧\巴兹”。

#3


1  

Here is an example, with anonymous function and by reference

这是一个示例,具有匿名函数和引用

 $test = array();

 $test['testFunction'] = function($message) {
  echo $message;
 };


 function show_msg($message) {
     echo $message;
 }

 $test['testFunction2'] = 'show_msg';

 call_user_func($test['testFunction'], 'Hi!');

 call_user_func($test['testFunction2'], 'Hello world!');
?>

#1


4  

As long as your PHP version is >= 5.3 you'll be able to harness anonymous functions and regular functions in your array:

只要您的PHP版本> = 5.3,您就可以在数组中使用匿名函数和常规函数:

function yourFunction($string){
    echo $string . " : by reference";
};

$array = array(
    'a' => function($string){
        echo $string;
    },
    'b' => 'yourFunction',
);

You can use the call_user_func or call_user_func_array functions.

您可以使用call_user_func或call_user_func_array函数。

call_user_func($array['a'], 'I love things');
call_user_func($array['b'], 'I love things');

Or As @Andrew stated in the comments too, you could call it as the following:

或者正如@Andrew在评论中所述,您可以将其称为以下内容:

$array['a']('I love things');
$array['b']('I love things');

If you'd like to read more about these callback methods, it's filed under the callback pseudo-type documentation on PHP.net and it is well worth a read!

如果您想了解有关这些回调方法的更多信息,请在PHP.net上的回调伪类型文档下提交,这非常值得一读!

#2


2  

Yes, you can do that, however you can't store the function as literal, but instead you have to refer to it by its name:

是的,您可以这样做,但是您不能将该函数存储为文字,而是您必须通过其名称来引用它:

$array = ['fun1', function () { /*do something*/ }, 'fun3'];

foreach ($array as $fun) {
    $fun();
}

Note that because it's by name, you'll have to use the fully qualified name should your function happen to be in a namespace; e.g. 'foo\bar\baz'.

请注意,因为它是按名称,如果您的函数恰好位于命名空间中,则必须使用完全限定名称;例如“富\酒吧\巴兹”。

#3


1  

Here is an example, with anonymous function and by reference

这是一个示例,具有匿名函数和引用

 $test = array();

 $test['testFunction'] = function($message) {
  echo $message;
 };


 function show_msg($message) {
     echo $message;
 }

 $test['testFunction2'] = 'show_msg';

 call_user_func($test['testFunction'], 'Hi!');

 call_user_func($test['testFunction2'], 'Hello world!');
?>