The PHP development team announces the immediate availability of PHP 7.4.0. This release marks the fourth feature update to the PHP 7 series.
PHP 7.4.0 comes with numerous improvements and new features such as:
- Typed Properties
- Arrow Functions
- Limited Return Type Covariance and Argument Type Contravariance
- Unpacking Inside Arrays
- Numeric Literal Separator
- Weak References
- Allow Exceptions from __toString()
- Opcache Preloading
- Several Deprecations
- Extensions Removed from the Core
For source downloads of PHP 7.4.0 please visit our downloads page Windows binaries can be found on the PHP for Windows site. The list of changes is recorded in the ChangeLog.
The migration guide is available in the PHP Manual. Please consult it for the detailed list of new features and backward incompatible changes.
Many thanks to all the contributors and supporters!
New Features
PHP Core
Typed properties
Class properties now support type declarations.
<?php
class User {
public int $id;
public string $name;
}
?>
Arrow functions
Arrow functions provide a shorthand syntax for defining functions with implicit by-value scope binding.
<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
// $nums = array(10, 20, 30, 40);
?>
Limited return type covariance and argument type contravariance
The following code will now work:
<?php
class A {}
class B extends A {}
class Producer {
public function method(): A {}
}
class ChildProducer extends Producer {
public function method(): B {}
}
?>
Null coalescing assignment operator
<?php
$array['key'] ??= computeDefault();
// is roughly equivalent to
if (!isset($array['key'])) {
$array['key'] = computeDefault();
}
?>
Unpacking inside arrays
<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
?>
Numeric literal separator
Numeric literals can contain underscores between digits.
<?php
6.674_083e-11; // float
299_792_458; // decimal
0xCAFE_F00D; // hexadecimal
0b0101_1111; // binary
?>
Weak references
Weak references allow the programmer to retain a reference to an object that does not prevent the object from being destroyed.
Allow exceptions from __toString()
Throwing exceptions from __toString() is now permitted. Previously this resulted in a fatal error. Existing recoverable fatal errors in string conversions have been converted to Error exceptions.
CURL
CURLFile now supports stream wrappers in addition to plain file names, if the extension has been built against libcurl >= 7.56.0.
Filter
The FILTER_VALIDATE_FLOAT
filter now supports the min_range and max_range options, with the same semantics as FILTER_VALIDATE_INT
.
FFI
FFI is a new extension, which provides a simple way to call native functions, access native variables, and create/access data structures defined in C libraries.
GD
Added the IMG_FILTER_SCATTER
image filter to apply a scatter filter to images.
Hash
Added crc32c hash using Castagnoli's polynomial. This CRC32 variant is used by storage systems, such as iSCSI, SCTP, Btrfs and ext4.
Multibyte String
Added the mb_str_split() function, which provides the same functionality as str_split(), but operating on code points rather than bytes.
Regular Expressions (Perl-Compatible)
The preg_replace_callback() and preg_replace_callback_array() functions now accept an additional flags
argument, with support for the PREG_OFFSET_CAPTURE
and PREG_UNMATCHED_AS_NULL
flags. This influences the format of the matches array passed to to the callback function.
PDO
The username and password can now be specified as part of the PDO DSN for the mysql, mssql, sybase, dblib, firebird and oci drivers. Previously this was only supported by the pgsql driver. If a username/password is specified both in the constructor and the DSN, the constructor takes precedence.
It is now possible to escape question marks in SQL queries to avoid them being interpreted as parameter placeholders. Writing ?? allows sending a single question mark to the database and e.g. use the PostgreSQL JSON key exists (?) operator.
PDO_OCI
PDOStatement::getColumnMeta() is now available.
PDO_SQLite
PDOStatement::getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT) allows checking whether the statement is read-only, i.e. if it doesn't modify the database.
PDO::setAttribute(PDO::SQLITE_ATTR_EXTENDED_RESULT_CODES, true) enables the use of SQLite3 extended result codes in PDO::errorInfo() and PDOStatement::errorInfo().
SQLite3
Added SQLite3::lastExtendedErrorCode() to fetch the last extended result code.
Added SQLite3::enableExtendedResultCodes($enable = true), which will make SQLite3::lastErrorCode() return extended result codes.
Standard
Custom object serialization
A new mechanism for custom object serialization has been added, which uses two new magic methods: __serialize and __unserialize.
<?php
// Returns array containing all the necessary state of the object.
public function __serialize(): array;
// Restores the object state from the given data array.
public function __unserialize(array $data): void;
?>
Array merge functions without arguments
array_merge() and array_merge_recursive() may now be called without any arguments, in which case they will return an empty array. This is useful in conjunction with the spread operator, e.g. array_merge(...$arrays).
proc_open() function
proc_open() now accepts an array instead of a string for the command. In this case the process will be opened directly (without going through a shell) and PHP will take care of any necessary argument escaping.
<?php
proc_open(['php', '-r', 'echo "Hello World\n";'], $descriptors, $pipes);
?>
proc_open() now supports redirect and null descriptors.
<?php
// Like 2>&1 on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['redirect', 1]], $pipes);
// Like 2>/dev/null or 2>nul on the shell
proc_open($cmd, [1 => ['pipe', 'w'], 2 => ['null']], $pipes);
?>
argon2i(d) without libargon
password_hash() now has the argon2i and argin2id implementations from the sodium extension when PHP is built without libargon.
Deprecated Features
PHP Core
Nested ternary operators without explicit parentheses
Nested ternary operations must explicitly use parentheses to dictate the order of the operations. Previously, when used without parentheses, the left-associativity would not result in the expected behaviour in most cases.
<?php
1 ? 2 : 3 ? 4 : 5; // deprecated
(1 ? 2 : 3) ? 4 : 5; // ok
1 ? 2 : (3 ? 4 : 5); // ok
?>
Array and string offset access using curly braces
The array and string offset access syntax using curly braces is deprecated. Use $var[$idx] instead of $var{$idx}.
(real) cast and is_real() function
The (real) cast is deprecated, use (float) instead.
The is_real() function is also deprecated, use is_float() instead.
Unbinding $this when $this is used
Unbinding $this of a non-static closure that uses $this is deprecated.
parent keyword without parent class
Using parent inside a class without a parent is deprecated, and will throw a compile-time error in the future. Currently an error will only be generated if/when the parent is accessed at run-time.
allow_url_include INI option
The allow_url_include ini directive is deprecated. Enabling it will generate a deprecation notice at startup.
Invalid characters in base conversion functions
Passing invalid characters to base_convert(), bindec(), octdec() and hexdec() will now generate a deprecation notice. The result will still be computed as if the invalid characters did not exist. Leading and trailing whitespace, as well as prefixes of type 0x (depending on base) continue to be allowed.
Using array_key_exists() on objects
Using array_key_exists() on objects is deprecated. Instead either isset() or property_exists() should be used.
Magic quotes functions
The get_magic_quotes_gpc() and get_magic_quotes_runtime() functions are deprecated. They always return FALSE
.
hebrevc() function
The hebrevc() function is deprecated. It can be replaced with nl2br(hebrev($str)) or, preferably, the use of Unicode RTL support.
convert_cyr_string() function
The convert_cyr_string() function is deprecated. It can be replaced by one of mb_convert_string(), iconv() or UConverter.
money_format() function
The money_format() function is deprecated. It can be replaced by the intl NumberFormatter functionality.
ezmlm_hash() function
The ezmlm_hash() function is deprecated.
restore_include_path() function
The restore_include_path() function is deprecated. It can be replaced by ini_restore('include_path').
Implode with historical parameter order
Passing parameters to implode() in reverse order is deprecated, use implode($glue, $parts) instead of implode($parts, $glue).
COM
Importing type libraries with case-insensitive constant registering has been deprecated.
Filter
FILTER_SANITIZE_MAGIC_QUOTES
is deprecated, use FILTER_SANITIZE_ADD_SLASHES
instead.
Multibyte String
Passing a non-string pattern to mb_ereg_replace() is deprecated. Currently, non-string patterns are interpreted as ASCII codepoints. In PHP 8, the pattern will be interpreted as a string instead.
Passing the encoding as 3rd parameter to mb_strrpos() is deprecated. Instead pass a 0 offset, and encoding as 4th parameter.
Lightweight Directory Access Protocol
ldap_control_paged_result_response() and ldap_control_paged_result() are deprecated. Pagination controls can be sent along with ldap_search() instead.
Reflection
Calls to ReflectionType::__toString() now generate a deprecation notice. This method has been deprecated in favor of ReflectionNamedType::getName() in the documentation since PHP 7.1, but did not throw a deprecation notice for technical reasons.
The export() methods on all Reflection classes are deprecated. Construct a Reflection object and convert it to string instead:
<?php
// ReflectionClass::export(Foo::class, false) is:
echo new ReflectionClass(Foo::class), "\n";
// $str = ReflectionClass::export(Foo::class, true) is:
$str = (string) new ReflectionClass(Foo::class);
?>
Socket
The AI_IDN_ALLOW_UNASSIGNED
and AI_IDN_USE_STD3_ASCII_RULES
flags for socket_addrinfo_lookup() are deprecated, due to an upstream deprecation in glibc.
Removed Extensions
These extensions have been moved to PECL and are no longer part of the PHP distribution. The PECL package versions of these extensions will be created according to user demand.
- Firebird/Interbase - access to an InterBase and/or Firebird based database is still available with the PDO Firebird driver.
- Recode
- WDDX