trait foo {
function foo() {
echo "bar";
}
}
trait bar {
use foo;
function bar() {
return $this->foo();
}
}
class global {
use test;
public $functions = ["bar"];
function callFunction($function) {
call_user_func($this->functions[$function]);
}
}
К сожалению, это единственное решение, которое я смог найти, и оно не сработает, если не вызовет фатальную ошибку и не сможет подавить ее с помощью @ перед любой из переменных выполнения. Итак, все еще здесь застряли ... Может кто-нибудь помочь мне решить это? Я могу скрыть отображение фатальных ошибок только с помощью ini_set ("display_errors", 0), который по-прежнему позволяет мне отображать другие типы ошибок
How do I store the function bar() as a reference value in $functions without evoking some kind of error such as "call to undefined function bar in globals"?
Ah! Found a solution thanks to zneak here: https://stackoverflow.com/a/16380836/784446
Essentially, the solution would store both traits and trait functions by array inside a functions array and would then be referenced by combining the elements into a single array, like so:
class global {
use test;
public $functions = ["classes"=>["foo", "bar"], "functions"=>["foo", "bar"]];
function callBar() {
$traitClass = $this->functions["classes"]["bar"];
$traitFunction = $this->functions["functions"]["bar"];
$barTrait = array($traitClass, "$traitFunction");
$barTrait();
}
}
Отдельное спасибо zneak за помощь в решении этого вопроса!