I'm trying to dynamically instantiate a class and executing a method by getting the class name and the method name from some variables.
This is the code i'm using:
public function processAPI() {
// Require the PHP file that containes the class
require_once(Settings\Path\Absolute::$engine."/class".$this->endpoint.".php");
// $this->endpoint is a string containing the class name (this is where i get the error, line 128)
$endpointClass = new $this->endpoint;
// $this->verb is the method (function) name
if(method_exists($endpointClass, $this->verb) > 0) {
// Executes the class method and returns it. $this->args is an array containing the arguments.
return $this->response(call_user_func_array($endpointClass->{$this->verb}, $this->args));
}
return $this->response('', 400);
}
I keep receiving the following error:
Fatal error: Class 'User' not found in D:\...\webname\resources\engine\classAPI.php on line 128
I also tried writing the whole code in the classic way and it's working without problems.
Useris inside a namespace and$this->endpointis not a fully qualified name? Keep in mind that if you have imported a classUserwithusethen you can straight-instantiate it withnew Userbut you cannot do the same withnew $classif$classis not fully qualified.classAPI.phpandclassUser.phpare both in the same namespace.__NAMESPACE__.'\'.$this->endpointas the class to instantiate and see.truei can change it to== true.