Added Exception for call instance methods when a Class constructor require parameters

pull/538/head
fadrian06 12 months ago
parent afba9c16a0
commit 6e29e663ee

@ -145,7 +145,7 @@ class Engine
];
foreach ($methods as $name) {
$this->dispatcher->set($name, [$this, '_' . $name]);
$this->dispatcher->set($name, [$this, "_$name"]);
}
// Default configuration settings

@ -7,6 +7,7 @@ namespace flight\core;
use Closure;
use Exception;
use InvalidArgumentException;
use ReflectionClass;
use TypeError;
/**
@ -193,7 +194,12 @@ class Dispatcher
*/
public static function execute($callback, array &$params = [])
{
if (is_string($callback) && !function_exists($callback)) {
$isInvalidFunctionName = (
is_string($callback)
&& !function_exists($callback)
);
if ($isInvalidFunctionName) {
throw new InvalidArgumentException('Invalid callback specified.');
}
@ -231,6 +237,23 @@ class Dispatcher
[$class, $method] = $func;
if (is_string($class) && class_exists($class)) {
$constructor = (new ReflectionClass($class))->getConstructor();
$constructorParamsNumber = 0;
if ($constructor !== null) {
$constructorParamsNumber = count($constructor->getParameters());
}
if ($constructorParamsNumber > 0) {
$exceptionMessage = "Method '$class::$method' cannot be called statically. ";
$exceptionMessage .= sprintf(
"$class::__construct require $constructorParamsNumber parameter%s",
$constructorParamsNumber > 1 ? 's' : ''
);
throw new InvalidArgumentException($exceptionMessage, E_ERROR);
}
$class = new $class();
}

Loading…
Cancel
Save