Skip to content

Instantly share code, notes, and snippets.

@MrMaksimize
Created February 16, 2012 18:20
Show Gist options
  • Save MrMaksimize/1846845 to your computer and use it in GitHub Desktop.
Save MrMaksimize/1846845 to your computer and use it in GitHub Desktop.
closure
<?php
$somefunction = function($foo, $bar){
//do something
}
$myfunction = ($somefunction($foo, $bar), $variable){
//do stuff
//call somefunction
//do some more stuff
}
@travist
Copy link

travist commented Feb 16, 2012

Maybe this...

$myfunction = (function(foo, bar) { 
  return function(foo, bar) {  
    // do something 
  };
})($foo, $bar);

@jamesiarmes
Copy link

You could do:

<?php
$somefunction = function($foo, $bar){
//do something
}

$myfunction = function($function, $variable){
$result = $function('foo', 'bar');
//do stuff
//call somefunction
//do some more stuff
}($somefunction, $variable)

@travist
Copy link

travist commented Feb 16, 2012

Ooops, I wrote my comment in javascript. In PHP, I am not sure why you can't just do this then...

function my_function($foo, $bar) {

}

$callback = 'my_function';
if (function_exists($callback)) {
  $callback('foo', 'bar');
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment