简体   繁体   中英

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

class 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

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? Some initial googling and php.net didn't turn up anything, but I might be using the wrong search terms.

This has changed in PHP 7 with the addition of the 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).

To quote the RFC, the list of globally reserved keywords in PHP <= 5.6.x is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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