Re: passing a function as a parameter

From: Date: Fri, 11 Jun 2004 14:43:22 +0000
Subject: Re: passing a function as a parameter
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-188106@lists.php.net to get a copy of this message
Torsten Roehr wrote:
"Bob Lockie" <bjlockie@lockie.ca> wrote in message news:40C9C0F2.3050208@lockie.ca...
I want to pass a function as a parameter and execute it in another
function.
A callback. :-) For example: function a() {
    echo "a";
} function b( $func ) {
    func();
    echo "b";
} The output of "b( a )" will be "ab".
Take a look at call_user_func(): http://de3.php.net/manual/en/function.call-user-func.php You have to pass the name of the function instead of the function itself: function a() {
     echo "a";
} function b($func) {
     call_user_func($func);
     echo "b";
} b('a'); Regards, Torsten
You can also do it this way: function b($func) { $func(); echo 'b'; } -- paperCrane <Justin Patrin>

« previous php.general (#188106) next »