Created
February 2, 2013 02:18
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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