<?php
//We set an error handler to throw an ErrorException whenever an error is triggered
set_error_handler(function (int $severity, string $message, string $filename, int $line) {
echo "Throwing ErrorException\n";
throw new ErrorException($message, 0, $severity, $filename, $line);
});
//We're going to test this error handler passing wrong arguments to array_diff
//array_diff compares array items CASTING them to string. Yes, it is retarded. No, it's not the lol here.
//http://php.net/manual/en/function.array-diff.php
//We declare an object that can't be cast to string
$stdObject = new stdClass();
//First case: let's trigger an error
try {
$hello = (string) $stdObject;
}catch(ErrorException $ee){
//Handler is called, exception is thrown and catched here. Yay!
echo "Catched exception: " . $ee->getMessage() . "\n";
}
echo "\n----------\n";
//Second case: let's trigger an error
try {
array_diff([], [$stdObject]);
}catch(ErrorException $ee){
//Handler is called, exception is thrown and catched here. Yay!
echo "Catched exception: " . $ee->getMessage() . "\n";
}
echo "\n----------\n";
//Third case: let's trigger a slightly different error
try {
array_diff([$stdObject], [$stdObject]);
}catch(ErrorException $ee){
//Handler is called, exception is thrown but... IT IS NOT CATCHED!
echo "Catched exception: " . $ee->getMessage() . "\n";
}