PHP7.4 New Features 新特性

- Core:

? . Added support for typed properties. For example:

? ? ? ? class User {

? ? ? ? ? ? public int $id;

? ? ? ? ? ? public string $name;

? ? ? ? }

? ? This will enforce that $user->id can only be assigned integer and

? ? $user->name can only be assigned strings. For more information see the

? ? RFC: https://wiki.php.net/rfc/typed_properties_v2

? . Added support for arrow functions with implicit by-value scope binding.

? ? For example:

? ? ? ? $factor = 10;

? ? ? ? $nums = array_map(fn($num) => $num * $factor, $nums);

? ? RFC: https://wiki.php.net/rfc/arrow_functions_v2

? . Added support for limited return type covariance and argument type

? ? contravariance. The following code will now work:

? ? ? ? class A {}

? ? ? ? class B extends A {}

? ? ? ? class Producer {

? ? ? ? ? ? public function method(): A {}

? ? ? ? }

? ? ? ? class ChildProducer extends Producer {

? ? ? ? ? ? public function method(): B {}

? ? ? ? }

? ? Full variance support is only available if autoloading is used. Inside a

? ? single file only non-cyclic type references are possible, because all

? ? classes need to be available before they are referenced.

? ? RFC: https://wiki.php.net/rfc/covariant-returns-and-contravariant-parameters

? . Added support for coalesce assign (??=) operator. For example:

? ? ? ? $array['key'] ??= computeDefault();

? ? ? ? // is roughly equivalent to

? ? ? ? if (!isset($array['key'])) {

? ? ? ? ? ? $array['key'] = computeDefault();

? ? ? ? }

? ? RFC: https://wiki.php.net/rfc/null_coalesce_equal_operator

? . Added support for unpacking inside arrays. For example:

? ? ? ? $arr1 = [3, 4];

? ? ? ? $arr2 = [1, 2, ...$arr1, 5];

? ? ? ? // $arr2 == [1, 2, 3, 4, 5]

? ? RFC: https://wiki.php.net/rfc/spread_operator_for_array

? . Support for WeakReferences has been added.

? ? RFC: https://wiki.php.net/rfc/weakrefs

? . Throwing exceptions from __toString() is now permitted. Previously this

? ? resulted in a fatal error. Existing recoverable fatals in string conversions

? ? have been converted to Error exceptions.

? ? RFC: https://wiki.php.net/rfc/tostring_exceptions

- CURL:

? . CURLFile now supports stream wrappers in addition to plain file names, if

? ? the extension has been built against libcurl >= 7.56.0.? The streams may

? ? need to be seekable.

- Filter:

? . The FILTER_VALIDATE_FLOAT filter now supports the min_range and max_range

? ? options, with the same semantics as FILTER_VALIDATE_INT.

- FFI:

? . A new extension which provides a simple way to call native functions, access

? ? native variables and create/access data structures defined in C libraries.

? ? RFC: https://wiki.php.net/rfc/ffi

- GD:

? . Added the "scatter" image filter (IMG_FILTER_SCATTER) to apply a scatter

? ? filter to images. This filter has the following prototype:

? ? ? ? imagefilter($im, IMG_FILTER_SCATTER, int $sub, int $plus, array $colors = []);

? ? The $colors array can be populated with a set of indexed colors where to

? ? apply the scatter pixel shifting on.

? ? Note, the result of this filter is always random.

- Hash:

? . Added "crc32c" hash using Castagnoli's polynomial. This crc32 variant is

? ? used by storage systems, such as iSCSI, SCTP, Btrfs and ext4.

- Mbstring:

? . Added mb_str_split() function, which provide the same functionality as

? ? str_split(), but operating on code points rather than bytes.

? ? RFC: https://wiki.php.net/rfc/mb_str_split

- OPcache:

? . Support for preloading code has been added.

? ? RFC: https://wiki.php.net/rfc/preload

- PCRE:

? . 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_OCI:

? . PDOStatement::getColumnMeta() is now available

- PDO_SQLite:

? . PDOStatement::getAttribute(PDO::SQLITE_ATTR_READONLY_STATEMENT) allows to

? ? check whether this statement is read-only, i.e. whether it doesn't modify

? ? the database.

- Standard:

? . strip_tags() now also accepts an array of allowed tags: Instead of

? ? strip_tags($str, '<a><p>') you can now write strip_tags($str, ['a', 'p']).

? . A new mechanism for custom object serialization has been added, which

? ? uses two new magic methods:

? ? ? ? // 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;

? ? The new serialization mechanism supersedes the Serializable interface,

? ? which will be deprecated in the future.

? ? RFC: https://wiki.php.net/rfc/custom_object_serialization

? . 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).

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容