(jp) =

これは、データ転送オブジェクトが何年にもわたってどのように進化してきたかを視覚化したかったので、私が書いた楽しい小さな投稿です。
よろしければ、私の 2 分間もご覧ください。
# 2014 年 8 月: PHP 5.6
PHP 5.6 から始めましょう。これは、現代の PHP の知識を持たないほとんどの人が、おそらく PHP コードがまだどのように見えるかを考えているものです。 コードを提供するだけで、将来のバージョンでの変更点について言及します。
class BlogData
private $title;
private $state;
private $publishedAt;
public function __construct(
$title,
$state,
$publishedAt = null
)
$this->title = $title;
$this->state = $state;
$this->publishedAt = $publishedAt;
public function getTitle()
return $this->title;
public function getState()
return $this->state;
public function getPublishedAt()
return $this->publishedAt;
# 2015 年 12 月: PHP 7.0
PHP 7.0 では、いくつかの主要な新しい構文機能が導入されました。ここで最も注目に値するのは、スカラー型と戻り型です。 Nullable 型はまだ実装されていないため、nullable には doc ブロック型を使用する必要があります。 $publishedAt:
class BlogData
private $title;
private $state;
private $publishedAt;
public function __construct(
string $title,
State $state,
$publishedAt = null
)
$this->title = $title;
$this->state = $state;
$this->publishedAt = $publishedAt;
public function getTitle(): string
return $this->title;
public function getState(): State
return $this->state;
public function getPublishedAt()
return $this->publishedAt;
# 2016 年 12 月: PHP 7.1
PHP 7.1 では、最終的に null 許容型が導入されたため、さらにいくつかの doc ブロックを削除できました。
class BlogData
private $title;
private $state;
private $publishedAt;
public function __construct(
string $title,
State $state,
?DateTimeImmutable $publishedAt = null
)
$this->title = $title;
$this->state = $state;
$this->publishedAt = $publishedAt;
public function getTitle(): string
return $this->title;
public function getState(): State
return $this->state;
public function getPublishedAt(): ?DateTimeImmutable
return $this->publishedAt;
# 2017 年 11 月: PHP 7.2
7.2 にはパラメーター型の拡張や object このリリースでは、特定の DTO をクリーンアップするためにできることは何もありません。
# 2018 年 12 月: PHP 7.3
同じことが PHP 7.3 にも当てはまります。ここでは何も表示されません。
# 2019 年 11 月: PHP 7.4
ただし、PHP 7.4 は別の話です。 型付きプロパティが追加されました — ついに!
class BlogData
private string $title;
private State $state;
private ?DateTimeImmutable $publishedAt;
public function __construct(
string $title,
State $state,
?DateTimeImmutable $publishedAt = null
)
$this->title = $title;
$this->state = $state;
$this->publishedAt = $publishedAt;
public function getTitle(): string
return $this->title;
public function getState(): State
return $this->state;
public function getPublishedAt(): ?DateTimeImmutable
return $this->publishedAt;
# 2020 年 11 月: PHP 8.0
もう 1 つのゲーム チェンジャー: PHP 8 はプロモートされたプロパティを追加します。 また、パラメーター リストの末尾のコンマが問題になりました。
class BlogData
public function __construct(
private string $title,
private State $state,
private ?DateTimeImmutable $publishedAt = null,
)
public function getTitle(): string
return $this->title;
public function getState(): State
return $this->state;
public function getPublishedAt(): ?DateTimeImmutable
return $this->publishedAt;
# 2021 年 11 月: PHP 8.1
次に、PHP 8.1 に到達します。 読み取り専用プロパティは重要であり、DTO を次のように記述できます。
class BlogData
public function __construct(
public readonly string $title,
public readonly State $state,
public readonly ?DateTimeImmutable $publishedAt = null,
)
# 2022 年 11 月: PHP 8.2
そしてついに、まだリリースされていない PHP 8.2 にたどり着きました。 クラスに読み取り専用プロパティしかない場合は常に、個々のプロパティごとではなく、クラス自体を読み取り専用としてマークできます。
readonly class BlogData
public function __construct(
public string $title,
public State $state,
public ?DateTimeImmutable $publishedAt = null,
)
それはかなりの違いだと思いませんか?
この言語がほぼ 10 年の間にどのように進化したかを見るのは興味深いことです。 10 年前に 8.2 構文を提案していたら、おそらく狂人と呼ばれていたでしょう。 今もそうですし、10年後の今を振り返ると「どうして我慢できたんだろう」と思うことでしょう。
//platform.twitter.com/widgets.js