note 113886 added to function.array-map
| From: | elfe1021 at gmail dot com | Date: | Fri, 13 Dec 2013 02:44:23 +0000 |
| Subject: | note 113886 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-197922@lists.php.net to get a copy of this message | ||
Find an interesting thing that in array_map's callable function, late static binding does not
work:
<?php
class A {
public static function foo($name) {
return 'In A: '.$name;
}
public static function test($names) {
return array_map(function($n) {return static::foo($n);}, $names);
}
}
class B extends A{
public static function foo($name) {
return 'In B: '.$name;
}
}
$result = B::test(['alice', 'bob']);
var_dump($result);
?>
the result is:
array (size=2)
0 => string 'In A: alice' (length=11)
1 => string 'In A: bob' (length=9)
if I change A::test to
<?php
public static function test($names) {
return array_map([get_called_class(), 'foo'], $names);
}
?>
Then the result is as expected:
array (size=2)
0 => string 'In B: alice' (length=11)
1 => string 'In B: bob' (length=9)
----
Server IP: 72.52.91.14
Probable Submitter: 72.52.91.14
----
Manual Page -- http://php.net/manual/en/function.array-map.php
Edit -- https://master.php.net/note/edit/113886
Del: integrated -- https://master.php.net/note/delete/113886/integrated
Del: useless -- https://master.php.net/note/delete/113886/useless
Del: bad code -- https://master.php.net/note/delete/113886/bad+code
Del: spam -- https://master.php.net/note/delete/113886/spam
Del: non-english -- https://master.php.net/note/delete/113886/non-english
Del: in docs -- https://master.php.net/note/delete/113886/in+docs
Del: other reasons-- https://master.php.net/note/delete/113886
Reject -- https://master.php.net/note/reject/113886
Search -- https://master.php.net/manage/user-notes.php