How to pass a function to a function?, and how to pass the variables of the function?

This is a discussion on How to pass a function to a function?, and how to pass the variables of the function? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; I'd like to write a function like: function f(){ ... bla ...} f_example(f); function f_example($function_to_execute) {...bla... $function_to_execute() ...bla....} AND ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 09-23-2005
_andrea.l
 
Posts: n/a
Default How to pass a function to a function?, and how to pass the variables of the function?

I'd like to write a function like:

function f(){ ... bla ...}
f_example(f);

function f_example($function_to_execute)
{...bla... $function_to_execute() ...bla....}

AND how to pass the variable of the function :

function f_example($function_to_execute($var1,$var2))
{...bla... $function_to_execute() ...bla....}

Thank you in advance for the time you'll spend for me,
Andrea.


Reply With Quote
  #2 (permalink)  
Old 09-23-2005
Kimmo Laine
 
Posts: n/a
Default Re: How to pass a function to a function?, and how to pass the variables of the function?

"_andrea.l" <andrea.lorizANTISPAM@libero.it> wrote in message
news:q3NYe.16114$Jr.257637@twister2.libero.it...
> I'd like to write a function like:
>
> function f(){ ... bla ...}
> f_example(f);
>
> function f_example($function_to_execute)
> {...bla... $function_to_execute() ...bla....}
>
> AND how to pass the variable of the function :
>
> function f_example($function_to_execute($var1,$var2))
> {...bla... $function_to_execute() ...bla....}
>
> Thank you in advance for the time you'll spend for me,


Actually just like that. :) Example:
<?php

function f_example($function_to_execute, $param){
// Let's first check function exists before calling it.
if(function_exists($function_to_execute))
$function_to_execute($param);
}

$myfunction = "print";
f_example($myfunction, "Hello World");

// This runs the function print and gives
// "Hello World" to it as a parameter,
// so it prints Hello World.

?>

There is also a function for this, it's called call_user_func(), you might
want to try that, but it's much more simple to just stick the function name
into a variable and call the function with the variable...

--
Welcome to Usenet! Please leave tolerance, understanding
and intelligence at the door. They aren't welcome here.
antaatulla.sikanautaa@gmail.com.NOSPAM.invalid


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:26 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0