简体   繁体   English

无效为类名称部分的PHP关键字列表

[英]List of PHP Keywords that are Invalid as Class Name Parts

I'm converting some old code that was written with "poor man's namespaces" like this, for PHP 5.2 我正在为PHP 5.2转换一些用“穷人的命名空间”编写的旧代码

class Foo_Bar_Model_Break_Baz{} Foo_Bar_Model_Break_Baz类{}

I'm converting this code for a new system that expects real namespaces, like this 我正在将此代码转换为需要真实名称空间的新系统,例如

namespace Foo\Bar\Model\Break;
class Baz
{
}

Unfortunately, PHP parser can't handle the above class because Break is a reserved word 不幸的是,PHP解析器无法处理以上类,因为Break是保留字

Parse error: syntax error, unexpected 'Break' (T_BREAK), expecting identifier (T_STRING) in

Is there a list of which parser tokens are invalid as part of a PHP full class name/namespace? 在PHP完整类名/命名空间中,有哪些解析器标记无效的列表? Some initial googling and php.net didn't turn up anything, but I might be using the wrong search terms. 一些最初的谷歌搜索和php.net并没有打开任何东西,但是我可能使用了错误的搜索词。

This has changed in PHP 7 with the addition of the Content Aware Lexer . 在PHP 7中,这已通过添加Content Aware Lexer进行了更改。 This RFC brings the number of reserved keywords down from 64 to 1, and that one is specifically a class constant named class (case insensitive). 该RFC将保留关键字的数量从64个减少到1个,其中一个特别是一个名为class的类常量(不区分大小写)。

To quote the RFC, the list of globally reserved keywords in PHP <= 5.6.x is: 要引用RFC,PHP <= 5.6.x中的全局保留关键字列表为:

callable  class  trait  extends  implements  static  abstract  final  public  
protected  private  const enddeclare  endfor  endforeach  endif  endwhile  and
global  goto  instanceof  insteadof  interface namespace  new  or  xor  try
use  var  exit  list  clone  include  include_once  throw  array print  echo  
require  require_once  return  else  elseif  default  break  continue  switch
yield function  if  endswitch  finally  for  foreach  declare  case  do  while
as  catch  die  self parent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM