当前位置: 动力学知识库 > 问答 > 编程问答 >

Parsing php code conditionally to avoid parse errors on older php versions

问题描述:

I have scripts that must be run with PHP 5.1.0 or higher (because using PDO class, for instance).

If not, they bring parse errors.

I'm talking of scripts that anonymous visitors are invited to download, install on their server (on which I can't have any information) and run to demonstrate how easy php scripts can communicate with a system of ours.

Of course, running any of these scripts and getting a "parse error at line xxx" won't really give a good image of the solution...

I tried echoing a neat "you need PHP 5.1.0 or higher to run this script" error message, using if (version_compare(PHP_VERSION, '5.1.0') < 0) { die(...); } but, of course, this doesn't work, as PHP first parses the whole script before starting executing it and having a chance to die on this test.

So my question is:

Is there a way in PHP to do some tests on php version BEFORE actually parsing?

I mean something like C/C++ #if or #ifdef PRE-processor directives.

Or any other type of workaround?

One constraint beeing: the solution to the issue should not make the whole scripts look unreadable or complex to understand (like massive calling of eval() which breaks syntax coloration) nor uncomprehensibly contrived (like having each script conditionaly included in a "container script"), because the global purpose is to demonstrate how SIMPLE it is to communicate with our system...

All solutions found googling around were about maintaining compatibility, using arrays of alternative functions to call depending on php version, comined with eval everywhere where something could go wrong for php parser... I would need something really simpler like "if php version not ok, don't parse"...

分享给朋友:
您可能感兴趣的文章:
随机阅读: