Question

Is there any easy way to use some variables (declared outside) inside the custom error function?

For example:

$a = "something";
$b = 123;

function errhnd($errno, $errstr, $errfile, $errline) {
  # need $a and $b vars here!
  }

set_error_handler('errhnd');

I can do it using globals and not sure is it a good way, is there anything else I can do to pass some data into my custom error-handler function?

Était-ce utile?

La solution

Use Global:

  # need $a and $b vars here!
  global $a;
  global $b;

  // Use them here!

or even shorter:

  global $a, $b;

Or use $GLOBALS[] like this:

  echo $GLOBALS['a'];
  echo $GLOBALS['b'];

http://www.php.net/manual/en/language.variables.scope.php

And you can pass them ofcourse like this:

function errhnd($a = NULL, $b= NULL, $errno, $errstr, $errfile, $errline) {
  # need $a and $b vars here!
  }

set_error_handler($a, $b, 'errhnd');
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top