<?php
class PreviousException extends \Exception {}
class One extends \RuntimeException {
// Just a standard exception
}
class Two extends \RuntimeException{
// An exception with custom constructor parameters
public function __construct($customMessage, PreviousException $e) {
parent::__construct($customMessage . "LOL", $e->getCode(), $e);
}
}
try {
throw new Two('Throwing two, but catch will receive a different class type', new One());
}
catch (\Exception $e) {
var_dump($e);
}
Fatal error: Uncaught TypeError: Two::__construct(): Argument #2 ($e) must be of type PreviousException, One given, called in /in/a3ErJ on line 19 and defined in /in/a3ErJ:13
Stack trace:
#0 /in/a3ErJ(19): Two->__construct('Throwing two, b...', Object(One))
#1 {main}
thrown in /in/a3ErJ on line 13
Process exited with code 255.
Fatal error: Uncaught TypeError: Argument 2 passed to Two::__construct() must be an instance of PreviousException, instance of One given, called in /in/a3ErJ on line 19 and defined in /in/a3ErJ:13
Stack trace:
#0 /in/a3ErJ(19): Two->__construct('Throwing two, b...', Object(One))
#1 {main}
thrown in /in/a3ErJ on line 13
Process exited with code 255.
Output for 5.6.38
Catchable fatal error: Argument 2 passed to Two::__construct() must be an instance of PreviousException, instance of One given, called in /in/a3ErJ on line 19 and defined in /in/a3ErJ on line 13
Process exited with code 255.