1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

php の予約語のキーワードは何の名前になら使えるのか

Last updated at Posted at 2020-04-20

PHP 7 系で newfor などの予約語がメソッド名などに使えるようになっているのは知っていたけど、イマイチその対象と範囲が理解できていなかったので手を動かして確認してみた。

Query::new()->for('foo')->as('hoge')->if(42)->and('bar')->or('baz')->list()->echo();

みたいなやつ。

結論としては、マニュアルに書いてあるとおりで、「これらのキーワードを プロパティや定数の名前として使える」(クラスやインタフェースやトレイトや関数の名前には使えない)という理解で間違いなさそう。

だだし、明記されているとおり class はメソッドやプロパティの名前には使えない。

default はクラス名・インタフェース名・トレイト名にだけ使えて、関数の名前には使えないのがおもしろい。

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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?