你能在 PHP 数组中存储一个函数吗?

例如:

$functions = array(
'function1' => function($echo) { echo $echo; }
);

这可能吗? 最好的选择是什么?

62303 次浏览

推荐的方法是使用 匿名函数:

$functions = [
'function1' => function ($echo) {
echo $echo;
}
];

如果你想存储一个已经声明的函数,那么你可以简单地通过名字把它称为字符串:

function do_echo($echo) {
echo $echo;
}


$functions = [
'function1' => 'do_echo'
];

在 PHP 的古老版本(< 5.3)中,不支持匿名函数,您可能需要使用 create_function(自 PHP 7.2以来已经废弃) :

$functions = array(
'function1' => create_function('$echo', 'echo $echo;')
);

所有这些方法都在 callable伪类型下的文档中列出。

无论您选择哪种方式,都可以直接调用该函数(PHP & ge; 5.4)或使用 call_user_func/call_user_func_array:

$functions['function1']('Hello world!');


call_user_func($functions['function1'], 'Hello world!');

警告 从 PHP 7.2.0开始就不赞成使用 create_function()。强烈建议不要依赖这个函数。

为了跟进 Alex Barrett 的文章,create _ function ()返回一个值,实际上可以用它来调用函数,这样:

$function = create_function('$echo', 'echo $echo;' );
$function('hello world');

自 PHP“5.3.0 Anonymous 函数可用”以来,使用示例如下:

请注意,这是远远快于使用旧的 create_function..。

//store anonymous function in an array variable e.g. $a["my_func"]
$a = array(
"my_func" => function($param = "no parameter"){
echo "In my function. Parameter: ".$param;
}
);


//check if there is some function or method
if( is_callable( $a["my_func"] ) ) $a["my_func"]();
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: no parameter"


echo "\n<br>"; //new line


if( is_callable( $a["my_func"] ) ) $a["my_func"]("Hi friend!");
else echo "is not callable";
// OUTPUTS: "In my function. Parameter: Hi friend!"


echo "\n<br>"; //new line


if( is_callable( $a["somethingElse"] ) ) $a["somethingElse"]("Something else!");
else echo "is not callable";
// OUTPUTS: "is not callable",(there is no function/method stored in $a["somethingElse"])

参考文献:

因为我可以..。

扩充 Alex Barrett 的职位。

我将进一步完善这个想法,甚至可能使用类似于外部静态类的东西,可能使用’...’标记来允许变长参数。

在下面的例子中,为了清楚起见,我使用了关键字“ array”,但是方括号也可以。显示的使用 init 函数的布局旨在演示更复杂代码的组织。

<?php
// works as per php 7.0.33


class pet {
private $constructors;


function __construct() {
$args = func_get_args();
$index = func_num_args()-1;
$this->init();


// Alex Barrett's suggested solution
// call_user_func($this->constructors[$index], $args);


// RibaldEddie's way works also
$this->constructors[$index]($args);
}


function init() {
$this->constructors = array(
function($args) { $this->__construct1($args[0]); },
function($args) { $this->__construct2($args[0], $args[1]); }
);
}


function __construct1($animal) {
echo 'Here is your new ' . $animal . '<br />';
}


function __construct2($firstName, $lastName) {
echo 'Name-<br />';
echo 'First: ' . $firstName . '<br />';
echo 'Last: ' . $lastName;
}
}


$t = new pet('Cat');
echo '<br />';
$d = new pet('Oscar', 'Wilding');
?>

好了,现在精简到一句话..。

function __construct() {
$this->{'__construct' . (func_num_args()-1)}(...func_get_args());
}

可用于重载任何函数,而不仅仅是构造函数。

通过使用闭包,我们可以在数组中存储函数。基本上闭包是一个可以在没有指定名称的情况下创建的函数——一个匿名函数。

$a = 'function';
$array=array(
"a"=> call_user_func(function() use ($a) {
return $a;
})
);
var_dump($array);

以下是对我有效的方法

function debugPrint($value = 'll'){


echo $value;
}


$login = '';




$whoisit = array( "wifi" => 'a', "login" => 'debugPrint', "admin" => 'c' );
 

foreach ($whoisit as $key => $value) {
     

if(isset($$key)) {
// in this case login exists as a variable and I am using the value of login to store the function I want to call
$value(); }
}