是否有一些函数用于检查PHP中的单词是否为reserved,还是我自己可以使用它?我可以手动检查:只需使用它并查看错误或警告,但我需要自动执行此检查。有没有办法做到这一点?
答案 0 :(得分:3)
您可以构建自己的自动化功能。 为此,请使用包含所有保留字的数组。Check out for more information
答案 1 :(得分:1)
是的,你可以。正如你在链接中提到的,我们有一个这个名字的列表,那么为什么要这样检查?:
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
var_dump(in_array('__halt_compiler',$keywords));
答案 2 :(得分:1)
借用http://www.php.net/manual/en/reserved.keywords.php
的数组您可以轻松修改它以适用于预定义的常量数组。
这有效。
<?php
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
$checkWord='break'; // <- the word to check for.
if (in_array($checkWord, $keywords)) {
echo "Found.";
}
else {
echo "Not found.";
}
?>
您还可以通过替换以下内容与表单一起实现:
$checkWord='break';
与
$checkWord=$_POST['checkWord'];
I.e。:
<?php
if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
$checkWord=$_POST['checkWord'];
if (in_array($checkWord, $keywords)) {
echo "FOUND!!";
}
else {
echo "Not found.";
}
}
?>
<form method="post" action="">
Enter word to check:
<input type="text" name="checkWord">
<br>
<input type="submit" name="submit" value="Check for reserved word">
</form>
在表单中使用两个数组的不同版本。
它可以代表一些抛光,但它可以解决问题
<?php
if(isset($_POST['submit'])){
$keywords = array('__halt_compiler', 'abstract', 'and', 'array', 'as', 'break', 'callable', 'case', 'catch', 'class', 'clone', 'const', 'continue', 'declare', 'default', 'die', 'do', 'echo', 'else', 'elseif', 'empty', 'enddeclare', 'endfor', 'endforeach', 'endif', 'endswitch', 'endwhile', 'eval', 'exit', 'extends', 'final', 'for', 'foreach', 'function', 'global', 'goto', 'if', 'implements', 'include', 'include_once', 'instanceof', 'insteadof', 'interface', 'isset', 'list', 'namespace', 'new', 'or', 'print', 'private', 'protected', 'public', 'require', 'require_once', 'return', 'static', 'switch', 'throw', 'trait', 'try', 'unset', 'use', 'var', 'while', 'xor');
$predefined_constants = array('__CLASS__', '__DIR__', '__FILE__', '__FUNCTION__', '__LINE__', '__METHOD__', '__NAMESPACE__', '__TRAIT__');
$checkWord=$_POST['checkWord'];
$checkconstant=$_POST['checkconstant'];
if (in_array($checkWord, $keywords)) {
echo "<b>Reserved word FOUND!!</b>";
echo "\n";
}
else {
echo "Reserved word not found or none entered.";
}
if (in_array($checkconstant, $predefined_constants)) {
echo "<b>Constant word FOUND!!</b>";
echo "\n";
}
else {
echo "Constant not found or none entered.";
}
}
?>
<form method="post" action="">
Enter reserved word to check:
<input type="text" name="checkWord">
Enter constant word to check:
<input type="text" name="checkconstant">
<br><br>
<input type="submit" name="submit" value="Check for reserved words">
<input type="reset" value="Reset" name="reset">
</form>
答案 3 :(得分:1)
我写了这个函数 isRW(string $string) NULL|false|string
试试看.. 如果输入不是单词,它将返回 NULL
,如果输入不是保留的,它将返回一个布尔值 false
,并且一个 string
(PHP 词法标记名称) 如果输入被保留..
function isRW($str){
if(!preg_match("/^\w{1,20}/", $str)) return null; $rsv=false;
$uc = ['true','false','parent','self']; //uncheckable list
$tk = token_get_all('<?php '.(boolval($str)? $str:'x')); $tknm=token_name($tk[1][0]);
$cst = get_defined_constants(true); unset($cst['user']); global $phpcst;
$phpcst = !isset($phpcst)? array_merge(...array_column($cst, null)): $phpcst;
if($tknm!='T_STRING'
or in_array($str, $uc) //if it's uncheckable
or @settype($str, $str) //if it's a type like int,NULL,object..
or preg_match("/__\w/", $str) //PHP reserved all methods prefixed by "__" for future use
or array_key_exists($str, $phpcst) //if it's a PHP const
or (function_exists($str) and (new \ReflectionFunction($str))->isInternal())
or (class_exists($str) and (new \ReflectionClass($str))->isInternal())) $rsv=true;
return $rsv? $tknm: $rsv;
}
一些测试输出:
isRW('}'); //return null
isRW('foobar'); //return boolean false
isRW('void'); //return boolean false
isRW('isInternal'); //return boolean false
isRW('_call'); //return boolean false
isRW('__call'); //return string T_STRING
isRW('__halt_compiler'); //return string T_HALT_COMPILER
isRW('private'); //return string T_PRIVATE
isRW('__DIR__'); //return string T_DIR
isRW('global'); //return string T_GLOBAL
isRW('E_ERROR'); //return string T_STRING
isRW('DATE_ISO8601'); //return string T_STRING
isRW('PHP_SAPI'); //return string T_STRING
isRW('namespace'); //return string T_NAMESPACE
isRW('function'); //return string T_FUNCTION
isRW('ReflectionClass'); //return string T_STRING
isRW('abstract'); //return string T_ABSTRACT
isRW('self'); //return string T_STRING
isRW('array'); //return string T_ARRAY
isRW('is_array'); //return string T_STRING
isRW('callable'); //return string T_CALLABLE
isRW('isset'); //return string T_ISSET
isRW('and'); //return string T_LOGICAL_AND
isRW('echo'); //return string T_ECHO
答案 4 :(得分:0)
根据您的用例,检查 php 中的保留字可能比简单地在列表中查找值要复杂一些。
有两点需要注意:
相同的保留字在您的代码中可能有不同的行为,具体取决于这一点,让我们以 array
关键字为例。
另一个例子,namespace
__halt_compiler
是两个排除项之一,即使在 8.0 版中也不能在命名空间中使用如您所见,有许多细微差别,这取决于不同版本中的大量更改。
我有同样的需求并为此创建了一个库。它可以在复杂的用例中使用,以检查是否可以在特定 php 版本的特定位置使用保留字。 请看一下,也许它可以解决您的问题。