PHP 7.1.12 Released


これらのキーワードは、PHP では特別な意味があります。これらのいくつかは 関数やメソッドのようなものを表し、いくつかは定数のようなものを表す、 といったようになっていますが、実際にはそうではありません。実際には、 これらは言語を構成するものです。以下のキーワードはいずれも定数、クラス名、 関数名として使用することはできません。 これらを変数名として使用することは一般的には可能ですが、 混乱を生じる可能性があります。

PHP 7.0.0 以降は、これらのキーワードを プロパティや定数の名前として使えるようになりました。 また、クラスやインターフェイスそしてトレイトのメソッド名としても使えるようになりました。 しかし、class だけは例外で、これを定数名として使うことはできません。

PHP のキーワード
__halt_compiler() abstract and array() as
break callable (PHP 5.4 以降) case catch class
clone const continue declare default
die() do echo else elseif
empty() enddeclare endfor endforeach endif
endswitch endwhile eval() exit() extends
final finally (PHP 5.5 以降) for foreach function
global goto (PHP 5.3 以降) if implements include
include_once instanceof insteadof (PHP 5.4 以降) interface isset()
list() namespace (PHP 5.3 以降) new or print
private protected public require require_once
return static switch throw trait (PHP 5.4 以降)
try unset() use var while
xor yield (PHP 5.5 以降)
__CLASS__ __DIR__ (PHP 5.3 以降) __FILE__ __FUNCTION__ __LINE__ __METHOD__
__NAMESPACE__ (PHP 5.3 以降) __TRAIT__ (PHP 5.4 以降)
add a note add a note

User Contributed Notes 4 notes

martindilling at gmail dot com
4 years ago
RegEx to find all the keywords:

5 years ago
Here they are as arrays:

= 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__');

Along with get_defined_functions() and get_defined_constants(), this can be useful for checking eval() statements.
Thomas Hansen
1 year ago
Please note that reserved words are still not allowed to be used as namespace or as part of it:

namespace MyNameSpace\List;


This will fail with a Parse error:  syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)
8 years ago
There are some cases when you need to use a reserved keyword or language construct as a class method name. In this instance, there is very little chance of namespace conflicts (as the class itself acts as a namespace). If you try to define the method the old way, you will get an unexpected token error.

There is an unobtrusive, and very useful way to use a reserved keyword for a method name. For example, you want to define two class methods 'list' and 'unset' (these two are language builtins and normally not allowed for method names).

class MyClass
// Define MyClass::unset() with a different name, e.g. 'rm'
public function rm($arg)
/* code... */
// Define MyClass::list() with a different name, e.g. 'ls'
public function ls($arg = null)
/* code... */
// Now define a __call() method (requires PHP > 5.2.3 to take effect)
public function __call($func, $args)
        switch (
$this->ls((isset($args[0]))? $args[0]: null);
trigger_error("Call to undefined method ".__CLASS__."::$func()", E_USER_ERROR);
            die ();

The only caveat is that to use the long method names, you need PHP > 5.2.3. However, a nice feature is that if you are using an older version than 5.2.3, all of the __call() stuff is ignored and the class will behave as expected (in other words, it degrades gracefully).

You also need to be aware of the methods' expected arguments. MyClass::ls()'s argument is optional, so the extra isset() check is required. If your methods take more arguments, they will need to be manually dereferenced from the $args array, e.g. <?php return $this->my_func($args[0], $args[1], $args[2]);?> for 3 required arguments.

This is a nice trick, and can let you code better APIs for newer versions of PHP. However, if this script is to be run on older PHP installations, be very sure to use the short method names.
To Top