Whats new in PHP 8.4
- Ismail Jamil
- 24 Nov 2024
PHP 8.4 has arrived with exciting new features that make coding easier and boost performance. This article breaks down the key updates with clear examples, helping developers of all skill levels unde
PHP 8.4 has arrived with exciting new features that make coding easier and boost performance. This article breaks down the key updates with clear examples, helping developers of all skill levels understand and apply them effortlessly.
source PHP Supported Versions
PHP 8.4 adds new array functions that save you from writing manual loops.
$arrayNumbers = [1, 2, 3, 4, 5];
/**
* Find the first even number
*/
$firstEven = array_find($arrayNumbers, fn($n) => $n % 2 === 0);
echo $firstEven; // Output: 2
/**
* Check if any number is greater than 2
*/
$hasBigNumber = array_any($arrayNumbers, fn($n) => $n > 2);
var_dump($hasBigNumber); // Output: bool(true)
/**
* Check if all numbers are positive
*/
$allPositive = array_all($arrayNumbers, fn($n) => $n > 0);
var_dump($allPositive); // Output: bool(true)
You can now create an object and call a method on it immediately, without wrapping the instantiation in parentheses.
class Logger {
/**
* Logging info message
* @param $message string
*/
public function info(string $message): void {
echo $message;
}
/**
* Logging error message
* @param $message string
*/
public function error(string $message): void {
echo $message;
}
}
/**
* Create an object and call a method in one step
*/
new Logger()->info('Logging info message'); // Output: Logging info message
Property hooks let you customise what happens when you get or set a property. This removes the need for separate getter and setter methods.
class User {
private string $name;
private int $lastName;
public function __construct(string $name, string $lastName) {
$this->name = $name;
$this->lastName = $lastName;
}
/**
* This property combines first and last name
*/
public string $fullName {
get => $this->name . ' ' . $this->lastName;
set => [$this->name, $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 Smith
You can now set different levels of visibility for reading and writing a property. For example, a property can be readable by everyone but only writable by the class itself.
class Wallet {
/**
* Public read, private write
*/
public private(set) float $balance;
public function __construct(float $initialBalance) {
$this->balance = $initialBalance;
}
public function deposit(float $amount): void {
$this->balance += $amount;
}
}
$wallet = new Wallet(100.0);
echo $wallet->balance; // Output: 100
$wallet->deposit(50.0); // Adds 50 to the balance
echo $wallet->balance; // Output: 150
/**
* The following line will cause an error:
*/
$wallet->balance = 200.0;
PHP 8.4 requires you to explicitly declare when a parameter can be null
. This makes code easier to understand and maintain.
/**
* PHP 8.4 (Recommended):
*/
function process(?string $data = null) {
echo $data ?? 'No data provided';
}
Lazy objects let you delay creating an object until it's actually used, which can save resources.
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 printed
PHP 8.4 has arrived with exciting new features that make coding easier and boost performance. This article breaks down the key updates with clear examples, helping developers of all skill levels unde
When writing conditional logic in JavaScript, developers have multiple choices, including `if-else`, `switch-case`, and `object lookup`. Each method has its advantages and disadvantages depending on