- debug_backtrace: documentation ( source)
- get_debug_type: documentation ( source)
<?php
class Foo {}
class Bar {
public function firstTypeError ($passed) {
$trace = debug_backtrace();
throw new TypeError(__CLASS__ . "::" . __FUNCTION__ . "(): Argument #1 (\$passed) elements must be of type " . Baz::class . ", " . get_debug_Type($passed) . " given, called in " . $trace[0]["file"] . " on line " . $trace[0]["line"]);
}
public function secondTypeError ($passed) {
$trace = debug_backtrace();
throw new TypeError(__CLASS__ . "::" . __FUNCTION__ . "(): Argument #1 (\$passed) elements must be of type " . Baz::class . ", " . get_debug_Type($passed) . " given");
}
}
class Baz {}
$bar = new Bar();
try {
$bar->firstTypeError("test");
} catch (TypeError $e) {
echo $e->__toString();
}
echo "\n\n\n\n";
try {
$bar->secondTypeError("test");
} catch (TypeError $e) {
echo $e->__toString();
}