2. WHAT’S NEW IN PHP 7.4
SOURCES
‣ Brent Roose - What's new in PHP 7.4
‣ Carlo Daniele - What's new in PHP 7.4 (Features,
Deprecations, Speed)
‣ RFCs PHP 7.4
‣ Brent Roose - Preloading in PHP 7.4
‣ David Shafik - PHP Performance I: Everything You Need to
Know About OpCode Caches
6. WHAT’S NEW IN PHP 7.4 - FEATURES
ARRAY SPREAD OPERATOR
‣ An array pair prefixed by … will be expanded in places
during array definition
7. WHAT’S NEW IN PHP 7.4 - FEATURES
ARRAY SPREAD OPERATOR (ERRORS)
‣ In order to make the behavior consistent with argument
unpacking, string keys are not supported
‣ It’s not possible to unpack an array by reference
11. WHAT’S NEW IN PHP 7.4 - FEATURES
PRELOADING
‣ Preloading is controlled by just a single new php.ini directive: opcache.preload
‣ Using this directive we will specify a single PHP file which will perform the
preloading task
‣ Once loaded, this file is then fully executed - and may preload other files, either
by including them or by using the opcache_compile_file() function at module
initialization
‣ Preloaded files remain cached in opcache memory forever, modification of their
corresponding source files won’t have any effect without another server restart
‣ In order for files to be preloaded, their dependencies (interfaces, parent classes
and traits) must be preloaded too
12. WHAT’S NEW IN PHP 7.4 - FEATURES
PRELOADING (SYMFONY 4.4)
‣ Symfony 4.4 can generate a preloading file for your
application in the cache directory, that includes both the
environment and and kernel names:
/path/to/project/var/cache/prod/App_KernelProdContainer.preload.php
13. WHAT’S NEW IN PHP 7.4 - FEATURES
PRELOADING (PERFORMANCES)
‣ You could decide to only preload “hot classes” (classes
that are used often in your codebase
‣ Benjamin Morel’s benchmark shows that only loading
around 100 hot classes, actually yields better performance
gains than preloading everything (13% to 17%
performance increase)
14. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH PRELOADING (BACKWARD COMPATIBILITY)
‣ Since preloaded classes and functions are always
available, function_exists() and class_exists() always
return TRUE
‣ Incorrect usage on a server with more than one app could
result in failures; as different apps (or different versions of
the same app) may have the same class/function names in
different files, so if one version of the class is preloaded it
will prevent loading of any other version of that class
defined in a different file
16. WHAT’S NEW IN PHP 7.4 - FEATURES
VARIANCE
Within the type system, a typing rule or a type constructor is:
‣ covariant if it preserves the ordering of types (from more
specific to more generic)
e.g. Cat() is an Animal()
‣ contravariant if it reverses this ordering
e.g. an Animal() is a Cat()
‣ variant if covariant or contravariant
‣ invariant if neither of these applies
17. WHAT’S NEW IN PHP 7.4 - FEATURES
WITHOUT COVARIANT RETURNS AND CONTRAVARIANT PARAMETERS
‣ Until 7.3, PHP had mostly invariant return types and did not permit choosing
less specific parameter types or more specific return types even though these
substitutions were type-safe
18. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH COVARIANT RETURNS AND CONTRAVARIANT PARAMETERS
20. WHAT’S NEW IN PHP 7.4 - FEATURES
WITHOUT ARROW FUNCTIONS 2.0
‣ Anonymous functions in PHP can be quite verbose and
partly this is due to the need to manually import used
variables
21. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH ARROW FUNCTIONS 2.0
‣ When a variable used in the expression is defined in the
parent scope it will be implicitly captured by-value
22. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH ARROW FUNCTIONS 2.0
23. WHAT’S NEW IN PHP 7.4 - FEATURES
ARROW FUNCTIONS 2.0 (BACKWARD COMPATIBILITY)
Unfortunately the fn keyword must be a full keyword and not
just a reserved function name
27. WHAT’S NEW IN PHP 7.4 - FEATURES
NULL COALESCING 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.
29. WHAT’S NEW IN PHP 7.4 - FEATURES
WITHOUT TYPED PROPERTIES 2.0
30. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH TYPED PROPERTIES 2.0
31. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH TYPED PROPERTIES 2.0
‣ Property type declarations support all type declarations
supported by PHP, with the exception of void and callable
32. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH TYPED PROPERTIES 2.0 (VARIANCE)
‣ Property types are invariant, it means that the type of a non-private
property can not change during inheritance
‣ If the parent property is private, then the type may be changed
arbitrarily
33. WHAT’S NEW IN PHP 7.4 - FEATURES
WITH TYPED PROPERTIES 2.0 (STRICT TYPES)
‣ Just like parameter and return type declarations, property
types are affected by the strict_types directive
40. WHAT’S NEW IN PHP 7.4 - DEPRECATIONS
LEFT-ASSOCIATIVE TERNARY OPERATOR
‣ Unlike most other languages, the ternary operator in PHP
is left-associative rather than right-associative
‣ Using nested ternaries without explicit parentheses will
throw a deprecation warning
42. WHAT’S NEW IN PHP 7.4 - DEPRECATIONS
CONCATENATION PRECEDENCE
‣ It’s been a long standing issue that an (unparenthesized) expression + - and .
evaluates left-to-right
‣ The precedence of . + and - is equal, in PHP 8 . will have an inferior precedence
‣ A deprecation notice upon encountering an unparenthesized expression
containing an . before + or -
44. WHAT’S NEW IN PHP 7.4 - DEPRECATIONS
CURLY BRACE SYNTAX FOR STRINGS AND ARRAYS
‣ PHP allowed both square brackets and curly braces to be
used interchangeably for accessing array elements and
string offsets
46. WHAT’S NEW IN PHP 7.4 - DEPRECATIONS
A FEW MORE DEPRECATIONS
‣ The real type (rarely used float, double alias)
‣ Magic quotes legacy (get_magic_quotes_gpc() and
get_magic_quotes_runtime())
‣ array_key_exists() with objects
‣ money_format() which is not supported on all platforms;
nowadays NumberFormatter::formatCurrency() should
be used instead