What’s New in PHP 7.4
PHP 7.4 coming with all new features and updates on November 28th, 2019.
Features, updates, and deprecations
- Typed Properties 2.0
- Null Coalesce Equal (
??=
) Operator - Numeric Literal Separator
- Spread Operator in Array Expression
- Support for WeakReferences
- Support for Arrow Functions
Typed Properties 2.0
- PHP 7 greatly increased the power of PHP’s type system with an introduction of scalar types and return types. PHP 7.4 comes with declaration types for class properties, which is missing in previous PHP versions where developers have to instead use getter and setter methods to enforce type contracts.
- By using typed properties, you can easily declare type hints to the class variables and properties. PHP RFC for Typed Properties 2.0
- Also, static properties will also have declare types that were not allowed earlier, following the same declaration methods for class variables and properties.
PHP7.4 Adds Support For Runtime-Enforced Type Annotations For Declared Properties.
Without Typed Properties
/**
* Book class
*/
class Book
{
/**
* Book id
*
* @var int $id
*/
private $id;
/**
* Book title
*
* @var string $title
*/
private $title;
/**
* Book constructor
*
* @param integer $id
* @param string $title
*/
public function __construct(int $id, string $title)
{
$this->id = $id;
$this->title = $title;
}
/**
* Function to get book id
*
* @return integer
*/
public function getId(): int
{
return $this->id;
}
/**
* Function to set book id
*
* @param integer $id
* @return void
*/
public function setId(int $id): void
{
$this->id = $id;
}
/**
* Function to get book title
*
* @return string
*/
public function getTitle(): string
{
return $this->title;
}
/**
* Function to set book title
*
* @param string $title
* @return void
*/
public function setTitle(string $title): void
{
$this->title = $title;
}
}
With Typed Properties
/**
* Book class
*/
class Book
{
/**
* Book id
*
* @var int $id
*/
private int $id;
/**
* Book title
*
* @var string $title
*/
private string $title;
/**
* Book constructor
*
* @param integer $id
* @param string $title
*/
public function __construct(int $id, string $title)
{
$this->id = $id;
$this->title = $title;
}
}
Null Coalesce Equal (??=) Operator
- PHP 7.4 has also taken a null coalescing operator (??) to the next level after its introduction in PHP7.
- This RFC proposes the introduction of a new null coalescing assignment operator (??=). Coalescing operator(??) being a comparison operator, this new coalesce equal or ??= operator is an assignment operator.
If The Left Parameter Is Null, Assigns The Value Of The Right Parameter To The Left One. If The Value Is Not Null, Nothing Is Made.
Without Null Coalesce Equal (??=) Operator
$this->request->data['user']['password'] = $this->request->data['user']['password']
?? '#EastOrWestIndiaIsBest';
With Null Coalesce Equal (??=) Operator
$this->request->data['user']['password'] ??= '#EastOrWestIndiaIsBest';
Numeric Literal Separator
- We humans are not optimized for parsing long sequences of digits quickly. Lack of visual separators takes longer to read and debug the code which indirectly leads to unintended errors.
- This RFC proposes a feature to improve code readability. PHP 7.4 comes with a visual separator to solve it by supporting an underscore in numeric literals for separating groups of digits. PHP RFC for Numeric Literal Separator
$companyMarketCap = 1_000_000_000; // a billion!
$amount = 124_00; // $124 as cents
Enable Improved Code Readability By Supporting An Underscore In Numeric Literals To Visually Separate Groups Of Digits.
- Underscore separators can be used in all-numeric literal notations supported by PHP:
2.124_083e-8; // float
24_702_748; // decimal
0xCAFE_F00D; // hexadecimal
0b0101_1111; // binary
0137_041; // octal
Array spread operator
- PHP already supports Argument Unpacking (aka Spread Operator) since 5.6. PHP7.4 brings this feature to the array expression proposed in this RFC.
An Array Pair Prefixed By … Will Be Expanded In Places During Array Definition. Only Arrays And Objects Who Implement Traversable Can Be Expanded.
$colors = ['red', 'green'];
$moreColors = ['blue', 'orange', ...$colors, 'yellow'];
// ['blue', 'orange', 'red', 'green', 'yellow'];
Support for WeakReferences
- PHP 7.4 introduces the WeakReference class. Weak references are references to objects, which do not prevent them from being destroyed proposed in this PHP RFC: Weak References.
$object = new stdClass;
$weakRef = WeakReference::create($object);
var_dump($weakRef->get()); // object(stdClass)#1 (0) {}
unset($object);
var_dump($weakRef->get()); // null
First, var_dump returns an object, and after unset it, second var_dump returns null.
Weak References Allow The Programmer To Retain A Reference To An Object Which Does Not Prevent The Object From Being Destroyed; They Are Useful For Implementing Cache Like Structures.
Support for Arrow Functions
- For performing simple operations, Anonymous functions can be quite verbose and sometimes difficult to maintain in PHP.
- Anonymous functions using simple closures makes code hard to read and understand due to its syntactic boilerplate, and manually importing used variables.
- PHP 7.4 introduces Arrow Functions inspired from ES6. Arrow functions are short closures for performing simpler operations which improves code readability & understandability proposed in this RFC.
Without Arrow Function
//For finding cube for given array elements without arrow function
function cube($n){
return ($n * $n * $n);
}
$array = [1, 2, 3, 4, 5];
echo "<pre>";
print_r(array_map('cube', $array));
echo "</pre>";
With Arrow Function
//For finding cube for given array elements using arrow function
$array = [1, 2, 3, 4, 5];
echo "<pre>";
print_r(array_map(fn($n) => $n * $n * $n, $array));
echo "</pre>";
Output
[1, 8, 27, 64, 125]
Deprecations in PHP 7.4
In PHP 7.4, following functionalities are deprecated, proposed in this RFC and will be removed in PHP 8.
- The
real
type - Magic quotes legacy
array_key_exists
() with objectsFILTER_SANITIZE_MAGIC_QUOTES
filter- Reflection export`() methods
mb_strrpos
() with encoding as 3rd argumentimplode
() parameter order mix- Unbinding
$this
from non-static closures hebrevc
() functionconvert_cyr_string
() functionmoney_format
() functionezmlm_hash
() functionrestore_include_path
() functionallow_url_include
ini directive