Protecting one class from bad programming of another?

Is there a way in PHP to try to include a file, but if the file contains errors that prevent it from compiling, just to skip that file from inclusion?

+7
include php compiler-errors runtime include-guards
source share
3 answers

You can call php -l in the corresponding file. However, it will spread and slow down. it does not handle runtime errors such as die ().

test.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"; 

test-good.php:

 <?php print "here\n"; 

test-bad.php:

 <?php die( 

$ php test.php

 here finished 
+4
source share

A less ideal solution, I thought I would mention the offspring here. The original idea is here .

You can capture the E_PARSE error you would get with a bad β€œrequest” and pass it to the shutdown function. The idea is to suppress parsing errors ...

 register_shutdown_function('post_plugin_include'); @require 'bad_include.php'; 

Then do your main implementation after the fact.

 function post_plugin_include() { if(is_null($e = error_get_last()) === false) { // do something else } } 

As I said, less ideal, but interesting nonetheless.

+1
source share

Depending on the version of PHP you can use php_check_syntax () (pretty much the same as php -l).

But its moo point is really .. Either you need the things you are trying to include, or you do not include them.

0
source share

All Articles