index.php
<?php
function bar()
{
try {
$res = 'loading foo:';
include 'foo.php';
} finally {
return $res . 'finally.';
}
}
$res = bar() . " Why it's working???";
# php 7.1.33 is work
# php 7.4.21 is work
# php 8.0.8 is work
echo $res; // "loading foo:foo is load.finally. Why it's working???"
foo.php
<?php
$res .= 'foo is load.';
$smth = '';
$smth->getBar();
If in the foo.php will be syntax error this sample will be work without fatal error... but why?
It's not safe use this feature, what you know about it?
I want to use it in my project as "easy catch" for control require module files:
function bar()
{
$path = 'module.php';
$res = '';
ob_start();
try {
include $path;
$res = ob_get_clean();
} catch (Exception $e) {
$res = 'Module has error (' . $path . '): file:' . $e->getFile()
. ', line:' . $e->getLine()
. ', mess:' . $e->getMessage();
} finally {
ob_clean();
return $res ?: 'Module has Fatal Error (' . $path . ')';
}
}
echo bar();
it's work for Fatal Errors, but without backtrace :(
I try to find more information or samples for this feature