PHPの関数オブジェクトの実験
<?
class A {
private function make($message) {
return function($entry) use($message) {
static $counter = 0;
$counter++;
return "$message, $counter $entry";
};
}
function func() {
$members = array("hoge", "moge", "fuga", "piyo");
var_dump(array_map($this->make("hello"), $members));
echo "\nagein\n\n";
var_dump(array_map($this->make("hi"), $members));
}
}
$a = new A();
$a->func();
# array(4) {
# [0]=>
# string(13) "hello, 1 hoge"
# [1]=>
# string(13) "hello, 2 moge"
# [2]=>
# string(13) "hello, 3 fuga"
# [3]=>
# string(13) "hello, 4 piyo"
# }
#
# agein
#
# array(4) {
# [0]=>
# string(10) "hi, 1 hoge"
# [1]=>
# string(10) "hi, 2 moge"
# [2]=>
# string(10) "hi, 3 fuga"
# [3]=>
# string(10) "hi, 4 piyo"
# }