note 63279 deleted from function.array-map by cmb
| From: | cmb@php.net | Date: | Sun, 25 Aug 2019 09:09:05 +0000 |
| Subject: | note 63279 deleted from function.array-map by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-211999@lists.php.net to get a copy of this message | ||
Note Submitter: pcdinh at phpvietnam dot net
----
Hi benjaminhill,
You can apply a method of a instantiated class to array_maps as follows:
class Maths {
function addOne($input) {
return ($input + 1);
}
}
$maths = new Maths();
$sum = array_map(array($maths, \\\'addOne\\\'), array(1, 2));
// where $maths is the object which has been instantiated before and addOne is its method without
its own parameters
var_dump($sum);
The code fragment will return:
array
0 => 2
1 => 3
However, I love a syntax like this:
$sum = array_map($maths->addOne($this), array(1, 2));
where $this should be interpreted as each values extracted from the subsequent array, which in this
case is array(1, 2).
This syntax reminds me of Javascript syntax.
PHP\\\'s callback mechanism should be improved.