kphp
kphp copied to clipboard
Implement exception handling for null reference by interface
Hi!
Need to call shutdown function when null reference exception caused.
<?php
register_shutdown_function(function () {
$a = error_get_last();
if (!$a) return;//ini_set("display_errors", 0);
echo "\ncatch error\n";
});
try {
interface I {
public function f();
}
class C implements I {
public function f() {}
}
function f(I $i) {
$i->f();
}
f(null);
}catch(Throwable $e ){
echo "\ncatch ex\n";
}
echo "\ndone\n";
------- kphp result---------------------
[1659472461] [2705875] Warning: Critical error "call method(I::f) on null object" in file /home/comm/data/projs/kphp-test/kphp_out/kphp/o_23/I@@f.cpp on line 26
------- Stack Backtrace -------
(0) ./kphp_out/cli : php_error(char const*, ...)+0x9c [0x57fe7c]
(1) ./kphp_out/cli : f$I$$f(class_instance<C$I> const&)+0xdf [0x4b97e5]
(2) ./kphp_out/cli : f$src_testNullReferenceInterfacee745a436ffd789c9()+0x6d [0x4b98af]
(3) ./kphp_out/cli : f$src_testNullReferenceInterfacee745a436ffd789c9$run()+0xd [0x4b9697]
(4) ./kphp_out/cli : PHPScriptBase::run()+0xb1 [0x61e7e1]
(5) /lib/x86_64-linux-gnu/libc.so.6 : +0x5b510 [0x7fcd2a0bc510]
-------------------------------
[pid 2705875] [time 1659472461] in php_assert_handler (SIGRTMIN+1 signal)
[pid 2705875] [time 1659472461] php assert error
[2705875][2022-08-02 23:34:21.494522 php-runner.cpp 325] Critical error during script execution: php assert error
------- end result---------------------
------- php result---------------------
catch ex
done
------- php end result---------------------
Other case: need same behaviour for KPHP for null reference on class:
<?php
class Foo {
public $x = 10;
public function f() { echo $this->x; }
public static function get():?self
{
return null;
}
}
register_shutdown_function(function () {
$a = error_get_last();
if (!$a) return;//ini_set("display_errors", 0);
echo "\ncatch error\n";
});
$foo = Foo::get();
try {
$foo->f();
}catch(Throwable $e ){
echo "\ncatch ex\n";
}
echo "\ndone\n";
------- kphp result---------------------
[1659471062] [2702052] Warning: Trying to access property of null object
------- Stack Backtrace -------
(0) ./kphp_out/cli : php_warning(char const*, ...)+0x9c [0x57fddc]
(1) ./kphp_out/cli : class_instance<C$Foo>::warn_on_access_null() const+0x2e [0x4b9a52]
(2) ./kphp_out/cli : f$src_testNullReference97986cf4cc5b98a4()+0xb2 [0x4b97c8]
(3) ./kphp_out/cli : f$src_testNullReference97986cf4cc5b98a4$run()+0xd [0x4b940e]
(4) ./kphp_out/cli : PHPScriptBase::run()+0xb1 [0x61e7e1]
(5) /lib/x86_64-linux-gnu/libc.so.6 : +0x5b510 [0x7f0da06a1510]
-------------------------------
10
done
------- end result---------------------
------- php result---------------------
catch ex
done
------- php end result---------------------