Since its release in late 2020, PHP 8 has been a game changer. In this tutorial, I will walk through all the latest features with real-world examples of when I might choose to use them.
Let’s break it down: // Before PHP 8.0 class Client { private string $url; public function __construct(string $url) { $this->url = $url; }} class Client { public function __construct( private string $url, ) {}}
// Before PHP 8.1 class Method { public const GET = 'GET'; public const POST = 'POST'; public const PUT = 'PUT'; public const PATCH = 'PATCH'; } enum Method: string { case GET = 'GET'; case POST = 'POST'; case PUT = 'PUT'; case PATCH = 'PATCH'; }
// Before PHP 8.1 class Post { public function __construct() { protected string $title, protected string $content, } public function getTitle(): string { return $this->title; } public function getContent(): string { return $this->content; }} class Post { public function __construct() { public readonly string $title, public readonly string $content, }}