2

I have the following code:

set_error_handler(
    function ($errno, $errstr, $errfile, $errline, array $errcontex) {
        throw new Exception($errstr, 0);
    },
    E_ALL
);

class Foo
{
    public function asd(): int
    {
        return 0;
    }
}

class Bar
{
    public function asd(): int
    {
        return 1;
    }
}


$a = [
    1 => new Foo(),
    2 => new Bar(),
];

try {
    array_diff([], $a);
} catch (Throwable $exception)
{
    var_dump("11");
}

When I run it, it results in the following error: PHP Warning: Uncaught Exception: Object of class Foo could not be converted to string in /oms/bebe.php:5 Stack trace: .... That is, an exception doesn't seem to be caught despite set_error_handler is in place. What's even more weird, when I declare an $a array with a single element,

$a = [
    2 => new Bar(),
];

everything works fine, that is, an exception is caught and 11 is output.

How come? And how can I catch that exception with an arbitrary array element amount?

Vadim Samokhin
  • 3,378
  • 4
  • 40
  • 68

0 Answers0