Accept function as parameter in PHP Accept function as parameter in PHP php php

Accept function as parameter in PHP


It's possible if you are using PHP 5.3.0 or higher.

See Anonymous Functions in the manual.

In your case, you would define exampleMethod like this:

function exampleMethod($anonFunc) {    //execute anonymous function    $anonFunc();}


Just to add to the others, you can pass a function name:

function someFunc($a){    echo $a;}function callFunc($name){    $name('funky!');}callFunc('someFunc');

This will work in PHP4.


You can also use create_function to create a function as a variable and pass it around. Though, I like the feeling of anonymous functions better. Go zombat.