Backend Development 11 min read

Understanding the New Features of PHP 8.0

This article explains how to download and configure PHP 8, troubleshoot common installation issues, and provides detailed examples of PHP 8's major new features—including union types, the match expression, null‑safe operator, constructor property promotion, attributes, and named arguments—complete with code snippets.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Understanding the New Features of PHP 8.0

PHP 8 was officially released on November 26 2020, bringing a set of powerful language enhancements. The article first shows how to obtain the PHP 8 binaries from the official website ( https://www.php.net/ ) or via a custom integration tool, and how to place the downloaded source into the D:\phpstudy_pro\Extensions\php directory for use with the phpstudy panel.

If the installed PHP 8 returns a 502 error, the guide suggests checking the version with php -v and, if a VCRUNTIME140.dll compatibility warning appears, downloading the compatible runtime from the provided link.

<code>PHP Warning: 'C:\Windows\SYSTEM32\VCRUNTIME140.dll' 14.0 is not compatible with this PHP build linked with 14.28 in Unknown on line 0</code>

The article then dives into the six most useful new features.

1. Union Types

Union types allow a parameter or return type to accept multiple types. In PHP 7 this was only possible via annotations; PHP 8 enforces type checking at runtime.

Example:

<code>&lt;?php $name = match(2) { 1 => 'kaka', 2 => 'niuiniu', }; echo $name; // niuiniu </code>

When a value does not match any case and no default is provided, an UnhandledMatchError is thrown:

<code>&lt;?php $name = match(3) { 1 => 'kaka', 2 => 'niuniu', }; echo $name; // Uncaught UnhandledMatchError </code>

2. Match Expression

The match construct works like switch but without implicit break statements and supports strict comparison, default values, and returning values directly.

Multiple conditions and default value example:

<code>&lt;?php $method = $_SERVER['REQUEST_METHOD']; match($method) { 'post' => $this->handlePost(), 'get', 'put' => $this->handleGet(), default => $this->handleDefault(), }; </code>

3. Null‑Safe Operator

PHP 8 introduces the ?-> operator to safely access properties or methods on potentially null objects, eliminating verbose null checks.

Legacy PHP 7 code:

<code>&lt;?php if ($session !== null) { $user = $session->user; if ($user !== null) { $address = $user->getAddress(); if ($address != null) { $country = $address->country; if ($country !== null) { var_dump($country); } } } } </code>

PHP 8 equivalent:

<code>&lt;?php $session = new Person(); echo $session?->user?->getAddress()?->country; </code>

4. Constructor Property Promotion

Properties can be declared and initialized directly in the constructor signature.

PHP 7 style:

<code>&lt;?php class User { public string $kaka; public function __construct(string $kaka) { $this->kaka = $kaka; } } </code>

PHP 8 style:

<code>&lt;?php class User { public function __construct(public string $kaka) { echo $this->kaka; } } </code>

5. Attributes (Annotations)

PHP 8 replaces doc‑block parsing with native attributes, providing a structured way to attach metadata.

Old reflection‑based approach:

<code>&lt;?php /** * @api http://www.kaka.com/api */ function show($name) {} $re = new ReflectionClass(new User); $doc = $re->getMethod('show')->getDocComment(); $res = substr($doc, strpos($doc, "@api") + strlen("@api "), -2); var_dump($res); // string(32) "http://www.kaka.com/api " </code>

New attribute syntax:

<code>&lt;?php #[api("http://www.kaka.com/api")] function show($name) {} $ref = new ReflectionFunction("show"); $attr = $ref->getAttributes("api")[0]; $value = $attr->getArguments(); var_dump($value[0]); // string(24) "http://www.kaka1.com/api" </code>

6. Named Arguments

Functions can now be called with arguments identified by name, allowing re‑ordering and skipping of optional parameters.

Positional call (PHP 7):

<code>&lt;?php class User { public function paramTest($name, $age) { var_dump($name . $age); } } $user = new User(); $user->paramTest('咔咔', 24); // string(8) "咔咔24" </code>

Named argument call (PHP 8):

<code>&lt;?php $user->paramTest(age: '咔咔', name: 24); // string(8) "24咔咔" </code>

Variadic parameters can also be used with named arguments, but positional arguments are no longer allowed once a named argument is used:

<code>&lt;?php class User { public function paramTest($name, $age, ...$other) { var_dump($other); } } $user = new User(); $user->paramTest(age: '咔咔', name: 24, sex: 1, like: "篮球"); // array(2) { ["sex"]=> int(1) ["like"]=> string(6) "篮球" } </code>

The article concludes that these features simplify code, improve type safety, and reduce boilerplate, while also noting that some reflection methods have been deprecated in PHP 8.

Backend DevelopmentUnion TypesattributesMatch ExpressionNamed ArgumentsNullsafe Operator
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.