note 100782 modified in function.call-user-func-array by vrana
| From: | vrana@php.net | Date: | Thu, 05 Jan 2012 05:59:09 +0000 |
| Subject: | note 100782 modified in function.call-user-func-array by vrana | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185368@lists.php.net to get a copy of this message | ||
It's not commented above:
This function behaves as call_user_func. To call a method of a class instace, instead of passing the
function name as the first argument, pass an array like ($classInstance, 'methodName')
<?php
public class Person {
private $name = null;
private $pocketContents= null;
protected function __construct() {
$this->name = 'My creator was too lazy to name me';
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getPocketContents() {
return $this->$pocketContents;
}
public function setPocketContents() {
$args = func_get_args();
$this->$pocketContents = $args;
}
}
$contents = array();
$contents[0] = 'random';
$contents['foo'] = 'bar';
$subobject = array();
$subobject['param'] = 'value';
$contents['subobject'] = $subobject;
call_user_func(($myInstance, 'setPocketContents'), $contents);
?>
--was--
It's not commented above:
This function behaves as call_user_func. To call a method of a class instace, instead of passing the
function name as the first argument, pass an array like ($classInstance, 'methodName)
<?php
public class Person {
private $name = null;
private $pocketContents= null;
protected function __construct() {
$this->name = 'My creator was too lazy to name me';
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getPocketContents() {
return $this->$pocketContents;
}
public function setPocketContents() {
$args = func_get_args();
$this->$pocketContents = $args;
}
}
$contents = array();
$contents[0] = 'random';
$contents['foo'] = 'bar';
$subobject = array();
$subobject['param'] = 'value';
$contents['subobject'] = $subobject;
call_user_func(($myInstance, 'setPocketContents'), $contents);
?>
http://php.net/manual/en/function.call-user-func-array.php