0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPのprotectedってなに?

Last updated at Posted at 2025-09-01
public function __construct(
    protected AppleMusic $apple, // ここがポイント
) {} // <- 結果として、本体に書くことがなくなった

引数の前に protected のような可視性(public, protected, private)を付けると、PHPは以下の3つのことを自動的にやってくれます。

protected AppleMusic $apple; というプロパティを宣言したとみなす。

__construct の引数として AppleMusic $apple を受け取るとみなす。

this->apple = $apple; という代入処理を裏で実行したとみなす。

つまり、従来の書き方で {} の中に書いていた処理を、引数の宣言部分が肩代わりしてくれるようになったのです。

その結果、開発者がメソッドの本体 {} の中に書くべきコードがなくなり、中身が空っぽの {} だけが残る、ということになります。

0
0
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?