У меня есть два класса A
и B
Я хочу «отправить» метод из A
в B
через B
конструктор, а затем выполнить его в B
.Я пытался работать с анонимными функциями так:
class A
{
public function __construct()
{
// Send testMethod() to B with an anonymous function
new B(function (string $test) {
$this->testMethod($test);
});
}
protected function testMethod(string $test) {
echo ($test);
}
}
class B
{
protected $testFct;
public function __construct($fctToExecute)
{
// Asign anonymous function to $testFct to be executed in all object
$this->testFct= function (string $test) {
$fctToExecute($test);
};
}
// Want to be able now to call this $testFct function in a method like :
protected function methodWhereICallTestfct() {
$this->testFct("I'm dumb!"); // It has to echo "I'm dumb!"
}
}
Но когда я пытаюсь настроить его, я всегда получаю сообщение об ошибке:
Uncaught Error: Call to undefined method testFct()
Ты хоть представляешь, в чем проблема?Я хотел бы указать, что моя версия PHP PHP 7.1.3
.
РЕДАКТИРОВАТЬ:
Здесь можно увидеть более похожий кодкак мои, которые возвращают ошибку