gpt4 book ai didi

PHP : anonymous function in associative array

转载 作者:行者123 更新时间:2023-12-02 09:20:29 25 4
gpt4 key购买 nike

有可能吗?类似的东西(不起作用):

$prototype = array(
'ext' => function ($args)
{
$ext = NULL;
if (in_array(func_get_arg(0), array('js', 'css')))
return $ext;
else
return 'js';
},
);

最佳答案

是的。唯一的限制是您无法将其转换为对象。

<?php
$foo = array(
'bar' => function($text)
{
echo $text;
}
);
$foo['bar']('test'); //prints "test"

$obj = (object)$foo;
$obj->bar('test'); //Fatal error: Call to undefined method stdClass::bar() in /code/REGnPf on line 11
?>

关于PHP : anonymous function in associative array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6997718/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com