PHP - pass an additional parameter (variable) to set_exception_handler

Is there a way to pass a variable to the set_exception_handler () method in PHP? I need something like this:

class Clazz { public /* static */ function foo() { set_exception_handler(array('Clazz', 'callback'), $var); // I need to pass $var // or this in non-static context $that = $this; set_exception_handler(array($that, 'callback'), $var); // I need to pass $var } public static function callback($exception, $var) { // process $exception using $var } } 
+4
source share
3 answers

As already mentioned in the comment, you should still use lambda functions:

  $lambda = function($exception) use ($var) { Clazz::callback($exception,$var); } set_exception_handler($lambda); 
+8
source

Use callback

 set_exception_handler(function($exception) use($var){ $that->callback($exception, $var); }); 
+2
source

One possibility is to catch the exception and throw the derivative that has this custom property.

 class MyLibraryException extends LibraryException { function __construct(LibraryException $e, $custom_field){ $this->custom_field = $custom_field; ... } } try { ... } catch(LibraryException $e) { new MyLibraryException($e, $cusotm_field); } 
+1
source

Source: https://habr.com/ru/post/1399883/


All Articles