You are here: Re: [PHP] Passing a function & arguments to a function « PHP « IT news, forums, messages
Re: [PHP] Passing a function & arguments to a function

Posted by Jochem Maas on 09/28/04 11:18

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.
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация