Answer the question
In order to leave comments, you need to log in
How to organize an instance of a class called through call_user_func ?
There was such a question.
I am writing a ranking system for the user, the description of each rank will be in a separate class.
$rangs - an array of all ranks
foreach($rangs as $rang) {
$alias = ucwords($rang['alias']);
// проверяем наличие класса для определения ранга
if(!class_exists($pathToRangsClass.$alias)) {
throw new \Exception('Не найден класс '.$pathToRangsClass.$alias);
}
$verify = call_user_func("\\modules\\rangs\\libraries\\{$alias}::verify", $userId);
}
public static function getInstance($className=__CLASS__)
{
return new $className;
}
$verify = call_user_func("\\modules\\rangs\\libraries\\{$alias}::getInstance", $userId);
$verify = call_user_func("\\modules\\rangs\\libraries\\{$alias}::getInstance()->verify", $userId);
Answer the question
In order to leave comments, you need to log in
Why do you like static methods so much? Why do you need classes then? Why not just spread the functions by namespace?
Try like this:
$verify = call_user_func(
[
"\\modules\\rangs\\libraries\\{$alias}",
"getInstance"
],
$userId
);
$verify = call_user_func([
"\\modules\\rangs\\libraries\\{$alias}",
"getInstance"
])->verify($userId);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question