©
Dieses Dokument verwendet PHP-Handbuch für chinesische Websites Freigeben
这些词语在 PHP 中有着特殊的意义。它们中有些像是函数,有些像是常量……但是它们不是的,它们只是语言结构的一部分。不能使用它们的任何一个作为常量、方法名或是类名。但是可以将它们作为变量名使用,不过这样会导致混淆。
and | or | xor | __FILE__ | exception (PHP 5) |
__LINE__ | array() | as | break | case |
class | const | continue | declare | default |
die() | do | echo | else | elseif |
empty() | enddeclare | endfor | endforeach | endif |
endswitch | endwhile | eval() | exit() | extends |
for | foreach | function | global | if |
include | include_once | isset() | list() | new |
require | require_once | return | static | |
switch | unset() | use | var | while |
__FUNCTION__ | __CLASS__ | __METHOD__ | final (PHP 5) | php_user_filter (PHP 5) |
interface (PHP 5) | implements (PHP 5) | extends | public (PHP 5) | private (PHP 5) |
protected (PHP 5) | abstract (PHP 5) | clone (PHP 5) | try (PHP 5) | catch (PHP 5) |
throw (PHP 5) | cfunction (PHP 4 only) | this (PHP 5 only) |
[#1] martindilling at gmail dot com [2013-04-15 22:20:27]
RegEx to find all the keywords:
\b(
(a(bstract|nd|rray|s))|
(c(a(llable|se|tch)|l(ass|one)|on(st|tinue)))|
(d(e(clare|fault)|ie|o))|
(e(cho|lse(if)?|mpty|nd(declare|for(each)?|if|switch|while)|val|x(it|tends)))|
(f(inal|or(each)?|unction))|
(g(lobal|oto))|
(i(f|mplements|n(clude(_once)?|st(anceof|eadof)|terface)|sset))|
(n(amespace|ew))|
(p(r(i(nt|vate)|otected)|ublic))|
(re(quire(_once)?|turn))|
(s(tatic|witch))|
(t(hrow|r(ait|y)))|
(u(nset|se))|
(__halt_compiler|break|list|(x)?or|var|while)
)\b
[#2] Chris [2012-10-04 16:33:07]
Here they are as arrays:
<?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__');
?>
Along with get_defined_functions() and get_defined_constants(), this can be useful for checking eval() statements.
[#3] Bob [2009-09-06 02:02:56]
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).
<?php
class MyClass
{
// Define MyClass::unset() with a different name, e.g. 'rm'
public function rm($arg)
{
}
// Define MyClass::list() with a different name, e.g. 'ls'
public function ls($arg = null)
{
}
// Now define a __call() method (requires PHP > 5.2.3 to take effect)
public function __call($func, $args)
{
switch ($func)
{
case 'list':
return $this->ls((isset($args[0]))? $args[0]: null);
break;
case 'unset':
return $this->rm($args[0]);
break;
default:
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.