note 113447 modified in language.types.callable by levim
| From: | levim@php.net | Date: | Thu, 05 Dec 2013 03:10:13 +0000 |
| Subject: | note 113447 modified in language.types.callable by levim | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-197742@lists.php.net to get a copy of this message | ||
When specifying a call back in array notation (ie. array($this, "myfunc") ) the method can
be private if called from inside the class, but if you call it from outside you'll get a
warning:
<?php
class mc {
public function go(array $arr) {
array_walk($arr, array($this, "walkIt"));
}
private function walkIt($val) {
echo $val . "<br />";
}
public function export() {
return array($this, 'walkIt');
}
}
$data = array(1,2,3,4);
$m = new mc;
$m->go($data); // valid
array_walk($data, $m->export()); // will generate warning
?>
Output:
1<br />2<br />3<br />4<br />
Warning: array_walk() expects parameter 2 to be a valid callback, cannot access private method
mc::walkIt() in /in/tfh7f on line 22
--was--
For those wondering, it seems that when specifiying a call back in object notation (ie. array($this,
"myfunc") ) that the method can be private.
Tested in php 5.3:
class mc {
public function go() {
$arr = array(1,2,3,4);
array_walk($arr, array($this, "walkIt"));
}
private function walkIt($val) {
echo $val . "<br />";
}
}
$m = new mc();
$m->go();
Output:
1
2
3
4
http://php.net/manual/en/language.types.callable.php