note 63279 added to function.array-map
| From: | pcdinh at phpvietnam dot net | Date: | Sat, 18 Mar 2006 04:50:57 +0000 |
| Subject: | note 63279 added to function.array-map | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-106011@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 64.71.164.2
Probable Submitter: 69.61.55.154
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.array-map.php
Edit -- http://master.php.net/note/edit/63279
Del: integrated -- http://master.php.net/note/delete/63279/integrated
Del: useless -- http://master.php.net/note/delete/63279/useless
Del: bad code -- http://master.php.net/note/delete/63279/bad+code
Del: spam -- http://master.php.net/note/delete/63279/spam
Del: non-english -- http://master.php.net/note/delete/63279/non-english
Del: in docs -- http://master.php.net/note/delete/63279/in+docs
Del: other reasons-- http://master.php.net/note/delete/63279
Reject -- http://master.php.net/note/reject/63279
Search -- http://master.php.net/manage/user-notes.php