<?php
function getMultiplier($product)
{
echo "kolikrat se spusti?"; // jen 1
return function ($value) use ($product) {
return $value * $product;
};
}
// $multi23 je "násobička 23"
$multi23 = getMultiplier(23);
echo $multi23(1);
echo $multi23(2);
echo $multi23(3);
echo $multi23(4);
<?php
class Dispatcher {
//Added explicit callback declaration.
var $callback;
public function Dispatcher( $callback ){
$this->callback = $callback;
}
public function asynchronous_method(){
//do asynch stuff, like fwrite...then, fire callback.
if ( isset( $this->callback ) ) {
if (function_exists( $this->callback )) call_user_func( $this->callback, "File done!" );
}
}
}
d = new Dispatcher( 'do_callback' );
$d->asynchronous_method();
function do_callback( $data ){
print 'Data is: ' . $data . "\n";
}
<?php
// This function uses a callback function.
function doIt($callback)
{
$data = "this is my data";
$callback($data);
}
// This is a sample callback function for doIt().
function myCallback($data)
{
print 'Data is: ' . $data . "\n";
}
// Call doIt() and pass our sample callback function's name.
doIt('myCallback');
<?php
function iWillReturnCallback($callBackHere){
$data = array('title' => 'Romes', 'result' => 'OK');
return $callBackHere($data);
}
function iAmCallBack($data){
return '<b>'.$data['result'].'</b>';
}
echo iWillReturnCallback(function($data){
return '<b>'.$data['result'].'</b>';
});
$luckyDip = array(
function() {
echo "You got a bag of toffees!";
},
function() {
echo "You got a toy car!";
},
function() {
echo "You got some balloons!";
}
);
$choice = rand( 0, 2 );
$luckyDip[$choice]();
<?php
class FileUpload
{
function save($callback)
{
$path = '/path/image';
echo "Data saved<br />";
//call the callback
$callback($path);
}
}
class Profile
{
function update_profile()
{
$f = new FileUpload('photo');
// Note, this syntax is very similar to javascript callback syntax
$f->save(function($path){
echo "Image resized at path $path <br />";
});
}
}
$p = new Profile;
$p->update_profile();