Reputation: 5004
I've custom code where I run class methods:
$object = new UserClass();
$method = 'create';
$params = ['name' => 'John'];
$reflectionMethod = new \ReflectionMethod($object, $method);
if($reflectionMethod->isStatic()) {
return $object::$method($params);
} else {
return $object->$method($params);
}
How I can run class method without checking if the method type is static or not, with one line if possible?
Upvotes: 0
Views: 83
Reputation: 47297
class UserClass {
public static function foo(string $name) {
echo 'hi ', $name, "\n";
}
public function bar(string $name) {
echo "bye ", $name, "\n";
}
}
$object = new UserClass();
$methods = ['foo', 'bar'];
foreach ($methods as $method) {
call_user_func([$object, $method], "Bobby");
}
Outputs:
hi Bobby
bye Bobby
call_user_func()
does not care if the method is static or not. It work in both cases.
Upvotes: 3