Re: Passing a function & arguments to a function

From: Date: Fri, 10 Jun 2005 23:51:44 +0000
Subject: Re: Passing a function & arguments to a function
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-216834@lists.php.net to get a copy of this message
David Johnson wrote:
I am trying to pass a function as an argument to another function, and then run the first function from within the second function. Example:
    function function0($arg0, $arg1, $arg2) {
        function2();
    }
        function3();
        do_function($arg5);
        function4();
    }
    function1(5, 100, function0($arg0, $arg1, $arg2));
    
<? function func0($arg0, $arg1, $arg2) { /* do stuff */ } function func1($arg1, $callback, $callbackArgs = /*order!*/array()) { func3(); func4(); if (is_callable($callback)) { $retval = call_user_func_array($callback, $callbackArgs); } } func1(5, 'func0', array($arg0, $arg1, $arg2)); ?> the 'func0' in : func1(5, 'func0', array($arg0, $arg1, $arg2)); could be something like: array('ClassName', 'MethodName') array($YourObject, 'MethodName')
In the above example, I am running function1(), which includes function0 as an argument. I am trying to pass "function0($arg0, $arg1, $arg2)" as an argument to function1, which will then execute the passed function, including its passed arguments. I have had mild success by splitting "function0($arg0, $arg1, $arg2)" into 2 parts of "function0" and "$arg0, $arg1, $arg2", and then passing both parts as an argument, such as $arg5 & $arg6, and then doing this:
    function function1($arg3, $arg4, $arg5) {
            $args_array = explode(", ",$arg6);
            $arg5($args_array[0],$args_array[1],$args_array[2]);
        }
    
However, this causes all of my arguments in the array to be evaluated as strings and not resources, which they may be. Also, I don't really like this method, and would prefer a much "cleaner" way of doing things.


« previous php.general (#216834) next »