如何调用要在 CodeIgniter 单元测试中使用的所有具有公用名的函数


How to call all functions with common name to be used in CodeIgniter Unit Test

很抱歉标题相当糟糕,但希望我可以用一些代码来解释它。

假设我有以下功能:

<?php    
function helloTest()
{
    echo 'hello';
}
function worldTest()
{
    echo 'world';
}
function helloworld()
{
    // call all functions with 'Test'
}
?>

helloworld 函数是否可以调用末尾命名为"Test"的所有函数?

$funcs = get_defined_functions();
foreach( $funcs['user'] as $f ) {
    if( strstr($f, 'Test') )
        call_user_func($f);
}

你应该使用Get All Methods php函数来获取所有方法,如下所示

function helloTest()
{
    echo 'hello';
}
function worldTest()
{
    echo 'world';
}
function helloworld()
{
    // call all functions with 'Test'
    $methods = get_defined_functions();
    $user_defined_methods = $methods['user'];
    foreach ($user_defined_methods as $method_name) 
    {
         //check with regular expressions if it's having 'Test' at end of $method_name then call that function using call_user_func($method_name)
    }
}

有关更多信息,请查看此链接