I'm trying to make a system which handles supplied data and calls the functions assigned to them in an array with addHandler().
Code:
class Test { public $arrHandlers = array(); public function addHandler($action, $function) { $this->arrHandlers[$action] = $function; } public function handleData($data) { $data = explode("/", $data); $action = array_shift($data); if(isset($this->arrHandlers[$action])) { call_user_func_array($this->arrhandlers[$action], array($data)); } } } function testFunc() { echo implode(" ", func_get_args()); } $obj = new Test(); $data = "egg/I/like/cheese"; $obj->addHandler("egg", "testFunc"); $obj->handleData($data); What it outputs:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given on line 13 What I want it to output:
I like cheese
$this->arrHandlers != $this->arrhandlers. Do you haveE_NOTICEdisplaying in yourerror_reporting? You should be getting an undefined propertyTest::$arrhandlersbefore the warning aboutcall_user_func_array()call_user_func_array($this->arrHandlers[$action], $data);.error_reporting(E_ALL)and fixed the caps mistake. This is what I now get:Notice: Array to string conversion on line 21 Array. Line 21 isecho implode(" ", func_get_args());arrHandlers. I've tested it working with that implementation.