New syntax must match the runtime that parses every application file. This project currently runs PHP 8.2, so the PHP 8.4 and 8.5 examples on this page are reference examples verified against official release documentation, not executable snippets for the local runtime.
After this lesson, you can recognize four important newer features, state their minimum versions, and design an upgrade that checks dependencies, static analysis, tests, deprecations, deployment images, and rollback compatibility before adopting syntax.
Property hooks can validate, normalize, or compute property access using syntax visible to the engine and static-analysis tools. A hook may be backed or virtual; property hooks cannot be combined with readonly properties.
<?php
final class Locale
{
public string $countryCode {
set(string $value) {
$this->countryCode = strtoupper($value);
}
}
}
$locale = new Locale();
$locale->countryCode = 'in';
echo $locale->countryCode;
IN
Asymmetric visibility makes a typed property readable more broadly than it is writable. The set visibility must be the same as or more restrictive than the read visibility.
<?php
final class Account
{
public function __construct(
public private(set) string $id,
) {}
}
$account = new Account('acct-42');
echo $account->id;
acct-42
The pipe operator sends the value on its left into the callable on its right, making a transformation chain read from left to right. Each right-hand expression must be callable and accept the piped value.
<?php
$title = ' PHP 8.5 Released ';
$slug = $title
|> trim(...)
|> (fn (string $value) => str_replace(' ', '-', $value))
|> (fn (string $value) => str_replace('.', '', $value))
|> strtolower(...);
echo $slug;
php-85-released
Clone-with syntax updates selected properties while cloning and is especially useful for immutable value objects. Constructor validation does not automatically rerun, so preserve class invariants in the class design.
<?php
readonly class Color
{
public function __construct(
public int $red,
public int $green,
public int $blue,
public int $alpha = 255,
) {}
}
$blue = new Color(79, 91, 147);
$transparent = clone($blue, ['alpha' => 128]);
echo $transparent->alpha;
128
0 of 2 checked
Try this next
0 of 2 completed
Explore 500+ free tutorials across 20+ languages and frameworks.