What's new in PHP 8.4
Published on 2024-11-23
What's new in PHP 8.4? Introduction
The latest version of PHP 8.4 was officially released on November 21, 2024, introducing several exciting features to improve developer experience and simplify codebases.
In this article, we'll highlight some of the most significant changes and enhancements in PHP 8.4, along with examples to help you get started.
Property Hooks with PHP 8.4
One of the most anticipated features in PHP 8.4 is Property Hooks. This feature allows developers to define custom behaviors for getting, setting, and initializing properties directly in the property declaration.
This drastically reduces boilerplate code and makes object-oriented programming in PHP more concise and expressive.
class AddressInfo
{
public string $city {
get => $this->city . ' city';
set {
if (!is_string($value)) {
throw new \InvalidArgumentException('Must be string.');
}
$this->city = $value;
}
}
}
For a detailed guide and examples, check out this article:
Asymmetric Visibility
Another interesting feature of PHP 8.4 is the asymmetric visibility.
This allows the class properties to define their visibility (public
, protected
, or private
), depending on the context of reading or writing.
class AddressVisible
{
public private(set) string $street = 'Street A';
}
$addressVisible = new AddressVisible();
echo $addressVisible->street;
// Street APHP Fatal error: "Uncaught Error: Cannot modify private(set) property AddressVisible::$street from global scope in"
//echo $addressVisible->street = 'Street A2';
See the full code example on my GitHub:
@s-damian - Asymmetric Visibility in PHP 8.4
New Without Parentheses with PHP 8.4
Another exciting feature in PHP 8.4 is the ability to instantiate objects without parentheses when no arguments are passed to the constructor.
This small but impactful change improves readability and simplifies chaining methods when working with new instances.
Here's how it works:
// With PHP 8.3 and (still valid with PHP 8.4):
$total = (new Math83(number1: 2, number2: 5))->addNumber(number: 12)->calculate();
// With PHP 8.4:
$total = new Math84(number1: 2, number2: 5)->addNumber(number: 12)->calculate();
As seen above, parentheses are no longer required when instantiating objects in cases where no arguments are provided.
This makes the code more concise and aligns PHP with the syntax of other modern programming languages.
See the full code example on my GitHub:
@s-damian - New Without Parentheses in PHP 8.4
Deprecated Attribute
With PHP 8.4 it is possible to use this attribute with the targets: function
, method
, class constant
.
Example:
class MathPhp
{
// Attribute "Deprecated" cannot target property.
private int $total = 0;
// Attribute "Deprecated" can target class constant.
#[\Deprecated]
public const CLASS_UTILITY = 'Math';
// Attribute "Deprecated" can target method.
#[\Deprecated]
public function calculate(): int
{
return $this->total;
}
}
See the full code example on my GitHub:
@s-damian - Deprecated Attribute in PHP 8.4
New Array Functions
With PHP 8.4 there are the new functions: array_find
, array_find_key
, array_all
and array_any
.
$arrayA = ['aaa', 'bbbbb', 'ccc', 'ddddd', 'eee'];
$arrayB = ['a' => 'aaa', 'b' => 'bbbbb', 'c' => 'ccc', 'd' => 'ddddd', 'e' => 'eee'];
$arrayC = [111, 222, 333, 444, 555];
$arrayD = [111, 222, 'bbb', 444, 555];
$test = array_find($arrayA, fn($value, $key) => mb_strlen($value) === 5);
var_dump( $test );
// string(5) "bbbbb"
$test = array_find_key($arrayB, fn($value, $key) => $key === 'b');
var_dump( $test ); // string(1) "b"
$test = array_all($arrayC, fn($value, $key) => is_numeric($value));
var_dump( $test ); // bool(true)
$test = array_any($arrayC, fn($value, $key) => is_numeric($value));
var_dump( $test ); // bool(true)
$test = array_any($arrayD, fn($value, $key) => is_numeric($value));
var_dump( $test ); // bool(true)
See the full code example on my GitHub:
@s-damian - New Array Functions in PHP 8.4
New mb Functions
With PHP 8.4 there are the new functions: mb_ucfirst
, mb_lcfirst
, mb_trim
, mb_ltrim
and mb_rtrim
.
var_dump( mb_ucfirst('football') );
// string(8) "Football"
var_dump( mb_lcfirst('FOOTBALL') );
// string(8) "fOOTBALL"
$text = '!Abc!';
var_dump( mb_trim($text, '!') );
// string(3) "Abc"
var_dump( mb_ltrim($text, '!') );
// string(4) "Abc!"
var_dump( mb_rtrim($text, '!') );
// string(4) "!Abc"
See the full code example on my GitHub:
@s-damian - New mb Functions in PHP 8.4
GitHub Repository
✨ If you found this tutorial useful, please give me a star on my GitHub repository: