I can not understand why this approach does not work
class MyClass { private $ErrorHandler = null; // Зарегистрировать обработчик ошибок public function RegisterErrorHandler($ErrorHandlerFn) { $this->ErrorHandler = $ErrorHandlerFn; } // Симуляция ошибки public function TestError(){ if($this->ErrorHandler) { // Если определен обработчик ошибок $this->ErrorHandler('Текст ошибки'); } } } $myClass = new MyClass(); $myClass->RegisterErrorHandler(function($errorText){ echo $errorText; }); $myClass->TestError(); While this code works perfectly
$ErrorHandler = function($errorText){ echo $errorText; }; $ErrorHandler('Текст ошибки');