<?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";
}
Fatal error: Uncaught Error: Object of class stdClass could not be converted to string in /in/BaUji:18
Stack trace:
#0 {main}
thrown in /in/BaUji on line 18
Process exited with code 255.
Output for 7.3.0 - 7.3.33
Throwing ErrorException
Catched exception: Object of class stdClass could not be converted to string
----------
----------
Throwing ErrorException
Warning: Uncaught ErrorException: Object of class stdClass could not be converted to string in /in/BaUji:38
Stack trace:
#0 [internal function]: {closure}(4096, 'Object of class...', '/in/BaUji', 38, Array)
#1 /in/BaUji(38): array_diff(Array, Array)
#2 {main}
thrown in /in/BaUji on line 38
Fatal error: Object of class stdClass could not be converted to string in /in/BaUji on line 38
Process exited with code 255.
Throwing ErrorException
Catched exception: Object of class stdClass could not be converted to string
----------
Throwing ErrorException
Catched exception: Object of class stdClass could not be converted to string
----------
Throwing ErrorException
Warning: Uncaught ErrorException: Object of class stdClass could not be converted to string in /in/BaUji:38
Stack trace:
#0 [internal function]: {closure}(4096, 'Object of class...', '/in/BaUji', 38, Array)
#1 /in/BaUji(38): array_diff(Array, Array)
#2 {main}
thrown in /in/BaUji on line 38
Fatal error: Object of class stdClass could not be converted to string in /in/BaUji on line 38
Process exited with code 255.
Output for 5.6.38 - 5.6.40
Catchable fatal error: Argument 1 passed to {closure}() must be an instance of int, integer given in /in/BaUji on line 4
Process exited with code 255.