codeigniter-php parameter is variable, how to invoke the method dynamically?

Source: Internet
Author: User
Such as:
I have three ways to do it now.

foo($a,$b=null)boo($a=array(),$b="")Roo($a)

Now I just need to pass the method name and the argument list to call the method directly.

Hopefully this is possible (pseudo code):

function run_function($fun_name,$paras){    {$fun_name}(....//这里应该怎么写呢?);}

I found the correct answer, thanks to the following two replies, but really not the answer I need. The solution is now posted:

$handler = array( 'test', 'test3');$params = array(    array(1,2,3,4),    1);if($params == null){    call_user_func($handler);}if (is_callable($handler) ){    call_user_func_array( $handler , $params );}class test{    function test1($arr,$arr1)    {        var_dump(array_merge($arr,$arr1));    }    function test2()    {        echo 123;    }    function  test3($arr,$a)    {        var_dump($arr);        var_dump($a);    }}

Reply content:

Such as:
I have three ways to do it now.

foo($a,$b=null)boo($a=array(),$b="")Roo($a)

Now I just need to pass the method name and the argument list to call the method directly.

Hopefully this is possible (pseudo code):

function run_function($fun_name,$paras){    {$fun_name}(....//这里应该怎么写呢?);}

I found the correct answer, thanks to the following two replies, but really not the answer I need. The solution is now posted:

$handler = array( 'test', 'test3');$params = array(    array(1,2,3,4),    1);if($params == null){    call_user_func($handler);}if (is_callable($handler) ){    call_user_func_array( $handler , $params );}class test{    function test1($arr,$arr1)    {        var_dump(array_merge($arr,$arr1));    }    function test2()    {        echo 123;    }    function  test3($arr,$a)    {        var_dump($arr);        var_dump($a);    }}

http://www.php.net/manual/zh/function.func-get-args.php

function run_function($fun_name){    if(function_exists($fun_name))    {        $num = func_num_args();                $args = func_get_args();        array_shift($args);        return $num==1 ? call_user_func($fun_name) : call_user_func_array($fun_name,$args);    }    trigger_error('Call to undefined function ' . $fun_name, E_USER_ERROR);}//使用run_function('foo', $a, $b);run_function('boo', $a, $b);run_function('Roo', $a);

The ignored answer can be fulfilled.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.