2月3日、Laravel 11に導入されたonce()というヘルパー関数に関する解説記事がLaravel Newsに投稿された。 2月3日、Laravel 11に導入された once()というヘルパー関数に関する解説記事がLaravel Newsに投稿された。 この新しいヘルパーは、 オブジェクトメソッドの呼び出し回数に関係なく、常に同じ値を得ることを保証 する。主に一度だけ実行されるべきコードがある場合、非常に便利である。 以下に、 once() を利用したコード例を示す。 <?php namespace App; use Illuminate\Support\Str; use Ramsey\Uuid\UuidInterface; class OnceDemo { public function uuid(): UuidInterface { return Str::uuid(); }
![知らなきゃ損!Laravel 11で導入された便利なヘルパー関数「once()」とは](https://cdn-ak-scissors.b.st-hatena.com/image/square/5b4c2802f186cf772c235329e53323de325a3c09/height=288;version=1;width=512/https%3A%2F%2Fres.cloudinary.com%2Ftechfeed%2Fimage%2Ffetch%2Fw_1200%2Ch_630%2Cc_fill%2Fhttps%253A%252F%252Fpicperf.io%252Fhttps%253A%252F%252Flaravelnews.s3.amazonaws.com%252Ffeatured-images%252Flaravel11-once-helper.jpg)