我同意@CatchAsCatchCan 和@El_Vanja 在评论中提出的观点。所有项目都应该有一个约定的 PHP 版本,并且所有贡献者都应该知道如何为每个项目设置他们的 PHP 版本。此外,所有更改都应由某个中央构建/CI 系统自动进行语法检查。如果缺少其中任何一个,那么实现这些都是时间和金钱的投资。
如果在达到这些目标之前,您需要一个额外的工具来帮助您排除编辑器的 PHP 语法检查器的故障,我制作了这个脚本:
<?php
// Check the syntax of this file with whatever tool you normally use
// (for example: your IDE / Sublime Text with a plugin / php -v <filename> )
// Compare the syntax error it reports with the comments below to
// determine which PHP version you have.
// PHP 8.0 will output:
// PHP Parse error: syntax error, unexpected token ";", expecting "(" in php_syntax_version.php on line 10
$a = match;
// PHP 7.4 will output:
// PHP Parse error: syntax error, unexpected 'fn' (T_FN), expecting identifier (T_STRING) in php_syntax_version.php on line 14
class fn{}
// PHP 7.3 will output:
// Parse error: Invalid body indentation level (expecting an indentation level of at least 3) in file on line 19
$str = <<<FOO
abcdefg
FOO
FOO;
// PHP 7.2 will output:
// PHP Fatal error: Cannot use 'object' as class name as it is reserved in php_syntax_version.php on line 25
class object{}
// PHP 7.1 will output:
// PHP Fatal error: Cannot use 'iterable' as class name as it is reserved in php_syntax_version.php on line 29
class iterable{}
// PHP 7.0 AND 5.6 will BOTH output (SEE BELOW):
// No syntax errors detected in php_syntax_version.php
// -------------------------------------------------------
// To tell between 5.6 and 7.0, uncomment these two lines:
// class C {}
// $c =& new C;
// PHP 7.0 will output:
// PHP Parse error: syntax error, unexpected 'new' (T_NEW) in php_syntax_version.php on line 39 !! Do not compare on the line number alone, as this can be misleading. !!
// PHP 5.6 will output:
// PHP Deprecated: Assigning the return value of new by reference is deprecated in php_syntax_version.php on line 39 !! Do not compare on the line number alone, as this can be misleading. !!