如何调用具有通用名称的所有函数以在CodeIgniter单元测试中使用

时间:2012-08-13 12:21:02

标签: php codeigniter

对于相当差的标题感到抱歉,但希望我可以用一些代码来解释它。

所以我想说我有以下功能:

<?php    
function helloTest()
{
    echo 'hello';
}

function worldTest()
{
    echo 'world';
}

function helloworld()
{
    // call all functions with 'Test'
}
?>

helloworld 函数是否可以将所有在末尾命名为“Test”的函数调用?

2 个答案:

答案 0 :(得分:2)

$funcs = get_defined_functions();

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

答案 1 :(得分:-1)

您应该使用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)
    }
}

了解详情this link