0

I need to put a contents of 'PHP Warning' into a variable. For these purposes I got the following code (set_error_handler() in a php class):

$data = '';
set_error_handler( function() use ($this, &$data) { $data = $this->my_error_handler(); return $data; } );
//some code that throws Warning
restore_error_handler();

function my_error_handler($errno, $errstr) {
    return $errstr;
}

But I get the following 500 error: Type error: Too few arguments to function my_error_handler(), 0 passed. If I replace a callable above with a string, code works without a 500 error, but I cannot pass a variable in it and return it with a contents of warning thrown. Any ideas how to fix it? Thank you.

Jack
  • 857
  • 14
  • 39
  • To work around the `Too few arguments` you could provide default values for the arguments and thus make them "optional" (https://stackoverflow.com/questions/34868/how-do-you-create-optional-arguments-in-php) – Capricorn Aug 06 '18 at 17:32
  • Try to throw exception and save the exception message into a variable. – Ahmed Numaan Aug 06 '18 at 17:57

0 Answers0