In PHP, how to call a function by its name as a string? The name strings may be known only at run time.
在PHP中,如何通过名称作为字符串来调用函数? 名称字符串只能在运行时知道。
If the string containing the function name is $func
, you may call the function like
如果包含函数名称的字符串是$func
,则可以像
$func()
or
要么
call_user_func($func)
using call_user_func()
.
使用call_user_func()
。
The call_user_func()
function supports more usage patterns, like
call_user_func()
函数支持更多使用模式,例如
call_user_func( array($obj,$func), $params )
to call object $obj
‘s function named $func
with parameters $params
.
使用参数$params
调用对象$obj
的名为$func
。
翻译自: https://www.systutorials.com/how-to-call-a-function-by-its-name-as-a-string-in-php/