LoginSignup
8
8

More than 5 years have passed since last update.

PHP7調査(34)型名と同じクラス名が作れないようになった

Last updated at Posted at 2015-05-23

PHP7から、組み込みの型名と同じクラス名・インターフェース名・trait名が作れなくなりました。

具体的には次の名前が予約されています。

  • int
  • float
  • bool
  • string
  • true
  • false
  • null
  • resource(PHP7.0時点ではエラーではなく、規約として利用禁止)
  • object(PHP7.0時点ではエラーではなく、規約として利用禁止)
  • mixed(PHP7.0時点ではエラーではなく、規約として利用禁止)
  • numeric(PHP7.0時点ではエラーではなく、規約として利用禁止)

これらは他の予約語とは異なり、変数名や関数名としては利用できます。

これは、PHP7から採用されたスカラ型のタイプヒントとの整合性の意味でも必要な修正だと言えるでしょう。intというタイプヒントがスカラ型を指しているのかユーザー定義クラスを指しているのか曖昧だと使い物になりませんからね。

コード例

<?php
class int {}
PHP Fatal error:  Cannot use 'int' as class name as it is reserved in /tmp/foo.php on line 2

直近の動向

voidも予約語にしようぜ、なんて話題がinternal MLで今も上がっています。

参照

8
8
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
8
8