Mar*_*lin 7 php compiler-errors runtime include-guards include
PHP中是否有一种方法可以尝试包含文件,但是如果文件包含阻止编译的错误,则只是从包含中跳过该文件?
您可以对相关文件调用 php -l 。不过,这会很费劲而且很慢。但它不处理像 die() 这样的运行时错误。
测试.php:
<?php
function check_code_file($filename)
{
$filename = escapeshellcmd($filename);
system("php -l $filename 2>/dev/null 1>/dev/null", $status);
if ($status) return false;
return true;
}
if (check_code_file('test-good.php'))
{
include('test-good.php');
}
if (check_code_file('test-bad.php'))
{
include('test-bad.php');
}
print "finished\n";
Run Code Online (Sandbox Code Playgroud)
测试-good.php:
<?php
print "here\n";
Run Code Online (Sandbox Code Playgroud)
测试-bad.php:
<?php
die(
Run Code Online (Sandbox Code Playgroud)
$ php 测试.php
here
finished
Run Code Online (Sandbox Code Playgroud)