New Features in PHP 8.4: Property Hooks, Asymmetric Visibility, Array Functions, Simplified Instantiation, Explicit Nullable Types, and Lazy Objects
PHP 8.4 introduces property hooks, asymmetric visibility, new array functions, simplified object instantiation, explicit nullable types, and lazy objects, providing cleaner syntax, better control, and performance improvements for developers across skill levels.
PHP 8.4 has been released, bringing several exciting features that simplify coding and improve performance.
1. Property Hooks
Property hooks let you customize what happens when a property is read or written, removing the need for separate getter and setter methods.
class user {
private string $firstname;
private string $lastname;
public function __construct(string $firstname, string $lastname) {
$this->firstname = $firstname;
$this->lastname = $lastname;
}
// this property combines first and last name
public string $fullname {
get => $this->firstname . ' ' . $this->lastname;
set => [$this->firstname, $this->lastname] = explode(' ', $value, 2);
}
}
$user = new user('john', 'doe');
echo $user->fullname; // output: john doe
$user->fullname = 'jane smith'; // updates first and last names
echo $user->fullname; // output: jane smithUsing property hooks makes code cleaner and reduces boilerplate.
2. Asymmetric Visibility
PHP 8.4 allows different visibility levels for reading and writing a property, e.g., a property can be publicly readable but only writable within the class.
class bankaccount {
public private(set) float $balance; // public read, private write
public function __construct(float $initialbalance) {
$this->balance = $initialbalance; // allowed here
}
public function deposit(float $amount): void {
$this->balance += $amount; // allowed here
}
}
$account = new bankaccount(100.0);
echo $account->balance; // output: 100
$account->deposit(50.0); // adds 50 to the balance
echo $account->balance; // output: 150
// the following line will cause an error:
// $account->balance = 200.0;This feature gives finer control over property access and updates.
3. New Array Functions
PHP 8.4 adds new array helper functions that eliminate manual loops.
$numbers = [1, 2, 3, 4, 5];
// find the first even number
$firsteven = array_find($numbers, fn($n) => $n % 2 === 0);
echo $firsteven; // output: 2
// check if any number is greater than 4
$hasbignumber = array_any($numbers, fn($n) => $n > 4);
var_dump($hasbignumber); // output: bool(true)
// check if all numbers are positive
$allpositive = array_all($numbers, fn($n) => $n > 0);
var_dump($allpositive); // output: bool(true)These functions make array manipulation faster and easier to read.
4. Simplified Object Instantiation
You can now create an object and immediately call a method without wrapping the instantiation in parentheses.
class logger {
public function log(string $message): void {
echo $message;
}
}
// create an object and call a method in one step
new logger()->log('logging a message'); // output: logging a messageThis reduces unnecessary syntax and keeps code tidy.
5. Explicit Nullable Types
PHP 8.4 requires explicit declaration when a parameter can be null, improving code clarity and safety.
// php 8.4 (recommended):
function process(?string $data = null) {
echo $data ?? 'no data provided';
}Explicit declarations prevent confusion and reduce potential errors.
6. Lazy Objects
Lazy objects defer creation until they are actually used, saving resources for expensive operations.
class ExpensiveResource {
public function __construct() {
// Simulate a time-consuming setup
sleep(2);
}
public function doWork(): void {
echo 'Working...';
}
}
// Use a lazy object to delay creation
$initializer = fn() => new ExpensiveResource();
$reflector = new ReflectionClass(ExpensiveResource::class);
$resource = $reflector->newLazyProxy($initializer);
// The object isn't created yet
$resource->doWork(); // Now the object is created and "Working..." is printedThis is especially useful when handling costly operations or large systems.
Conclusion
PHP 8.4 introduces multiple features—property hooks, asymmetric visibility, new array functions, simplified object instantiation, explicit nullable types, and lazy objects—that make coding simpler, more powerful, and more maintainable for developers of all skill levels.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.