Source: stitcher.io

PHP 8.1 in 8 code blocks

Category: PHP, PHP

Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. enum Status { case draft; case published; case archived; public function color(): string { return match($this) { Status::draft => 'grey', Status::published => 'green', Status::archived => 'red', }; }} https://stitcher.io/blog/php-enums class PostData { public function __construct( public readonly string $title, public readonly string $author, public readonly string $body, public readonly DateTimeImmutable $createdAt, public readonly PostState $state, ) {}} https://stitcher.io/blog/php-81-readonly-properties class PostStateMachine { public function __construct( private string $state = new Draft(), ) { }} https://stitcher.io/blog/php-81-new-in-initializers $fiber = new Fiber(function (): void { $valueAfterResuming = Fiber::suspend('after suspending'); }); $valueAfterSuspending = $fiber->start(); $fiber->resume('after resuming'); https://stitcher.io/blog/fibers-with-a-grain-of-salt"" Do you want to learn more about PHP 8.1? For the next 10 days, you'll receive a daily email covering a new and exiting feature of PHP 8.1; afterwards you'll be automatically unsubscribed, so no spam or followup. https://road-to-php.com/ $array1 = ["a" => 1]; $array2 = ["b" => 2]; $array = ["a" => 0,...$array1,...$array2]; var_dump($array); https://stitcher.io/blog/new-in-php-81#array-unpacking-with-string-keys-rfc function foo(int $a, int $b) { } $foo = foo(..).; $foo(a: 1, b: 2); https://wiki.php.net/rfc/first_class_callable_syntax function generateSlug(HasTitle&HasId $post) { return strtolower($post->getTitle()). $post->getId(); } https://stitcher.io/blog/new-in-php-81#pure-intersection-types-rfc $list = ["a", "b", "c"]; array_is_list($list); $notAList = [1 => "a", 2 => "b", 3 => "c"]; array_is_list($notAList); $alsoNotAList = ["a" => "a", "b" => "b", "c" => "c"]; array_is_list($alsoNotAList); https://stitcher.io/blog/new-in-php-81#new-array_is_list-function-rfc Noticed a tpyo?
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners