Help us understand the problem. What is going on with this article?

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

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

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

みたいなやつ。

https://www.php.net/manual/ja/reserved.keywords.php

https://wiki.php.net/rfc/context_sensitive_lexer

https://github.com/php/php-src/pull/1221/

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

だだし、明記されているとおり 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

https://github.com/youhey/php-reserved-keywords

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした