Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - pass an extra parameter (variable) to set_exception_handler

Is there any way to pass 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
    }
}
like image 378
Pavel S. Avatar asked Dec 17 '22 03:12

Pavel S.


2 Answers

As i already indicated in the comment you have to use lambda-functions anyway:

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

 set_exception_handler($lambda);
like image 82
androidavid Avatar answered Dec 27 '22 21:12

androidavid


Use a callback

set_exception_handler(function($exception) use($var){
    $that->callback($exception, $var);
});
like image 37
fredtma Avatar answered Dec 27 '22 19:12

fredtma