Skip to content

Instantly share code, notes, and snippets.

@jrmadsen67
Created February 2, 2013 02:18
Show Gist options
  • Save jrmadsen67/4695757 to your computer and use it in GitHub Desktop.
Save jrmadsen67/4695757 to your computer and use it in GitHub Desktop.
Playing around with callbacks in array_map. Notice array($this, 'process') lets you specify an object->method, perfect for use in framework controller (like in Codeigniter)
<?php
$main_object = new main_object();
$main_object->main();
class main_object{
function main()
{
$method = array($this, 'process');
var_dump($method);
$array = array(1,2,3,4);
array_map(array($this, 'process'), $array);
echo '<br />';
array_map(array(new other_object(), 'triple'), $array);
echo '<br />';
array_map(create_function('$n', 'echo $n * 2;'), $array);
}
function process($n)
{
echo $n;
}
}
class other_object
{
function triple($n)
{
echo $n * 3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment