I'm trying to call an object's non-static method with call_user_func_array but I'm not understanding how to formulate the callback. I've found a lot of similar examples online but nothing quite like what I'm running into.
class DBCommand {
private $db; // The DBConnection object
function __construct() {
$db = new DBConnection();
}
function callMethod($method, $arguments) {
// This line gives me the error:
return call_user_func_array(array($this->db, "$method"), $arguments);
}
}
?>
Calling callMethod with the name of a DBConnection method and its proper arguments gives me this
PHP Warning: call_user_func_array() expects parameter 1 to be a valid
callback, first array member is not a valid class name or object
And because of this, callMethod returns null.
$this->db = new DBConnection();in constructor. Currently you assign a connection object to just a local variable, not to an object field.$methodwill work.DBConnection.