note 109374 added to function.array-walk
| From: | mwwaygooAThotmailDOTcom | Date: | Wed, 11 Jul 2012 14:14:24 +0000 |
| Subject: | note 109374 added to function.array-walk | ||
| Groups: | php.notes php.notes | ||
| Request: | Send a blank email to php-notes+get-189102@lists.php.net to get a copy of this message | ||
further to paul at heliosville dot com and a correction of memandeemail at gmail dot com
Inside a class you can array walk a method, by supplying $this and the name of the method, as an
array for the second argument.
<?php
class test {
public function my_function(&$data) {
$data=1;
}
public function clean_data($data) {
array_walk($data, array($this, "my_function"));
return $data;
}
}
$a=new test();
$a->clean_data($my_array);
?>
NOTE: to reference the current method, you can use __FUNCTION__ instead of the literal name.
But avoid __METHOD__ as this will also include the name of the class (ie test::my_function), which
is not required for instantiated classes like this.
<?php
class test {
public function objectToArray(&$data)
{
if(is_object($data))
{
$data=get_object_vars($data);
}
if(is_array($data))
{
array_walk($data, array(&$this, __FUNCTION__));
}
}
}
$a=new test();
$a->objectToArray($my_object);
?>
----
Server IP: 94.76.225.145
Probable Submitter: 81.144.235.82
----
Manual Page -- http://www.php.net/manual/en/function.array-walk.php
Edit -- https://master.php.net/note/edit/109374
Del: integrated -- https://master.php.net/note/delete/109374/integrated
Del: useless -- https://master.php.net/note/delete/109374/useless
Del: bad code -- https://master.php.net/note/delete/109374/bad+code
Del: spam -- https://master.php.net/note/delete/109374/spam
Del: non-english -- https://master.php.net/note/delete/109374/non-english
Del: in docs -- https://master.php.net/note/delete/109374/in+docs
Del: other reasons-- https://master.php.net/note/delete/109374
Reject -- https://master.php.net/note/reject/109374
Search -- https://master.php.net/manage/user-notes.php