PHP 8.4 Features
Property Hooks
class User {
public string \$name {
set(string \$value) {
\$this->name = trim(\$value);
}
get => \$this->name;
}
// Implicit get
public string \$fullName =>
\$this->firstName . ' ' . \$this->lastName;
}
Asymmetric Visibility
class Post {
public private(set) string \$title;
public protected(set) DateTime \$createdAt;
public function __construct(string \$title) {
\$this->title = \$title;
\$this->createdAt = new DateTime();
}
}
// \$title is publicly readable but only privately settable
Lazy Objects
\$initializer = function (Service \$service): void {
\$service->initialize();
};
\$proxy = ReflectionLazyObject::newLazyProxy(
Service::class, \$initializer
);
// Service is only initialized when accessed
New Array Functions
array_find(\$items, fn(\$i) => \$i > 3); // first match
array_find_key(\$items, fn(\$i) => \$i > 3); // key of first match
array_any(\$items, fn(\$i) => \$i > 3); // any match?
array_all(\$items, fn(\$i) => \$i > 0); // all match?
Examples
<?php
$nums = [1, 2, 3, 4, 5];
// PHP 8.4 array functions
$firstOver3 = array_find($nums, fn($n) => $n > 3);
echo "First > 3: $firstOver3\n"; // 4
$hasLarge = array_any($nums, fn($n) => $n > 10);
echo "Any > 10: " . ($hasLarge ? 'yes' : 'no');
$allPositive = array_all($nums, fn($n) => $n > 0);
echo "All positive: " . ($allPositive ? 'yes' : 'no');