Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Top ten of PHP 7.4

88 views

Published on

The best new features introduced in the new PHP 7.4.

Published in: Internet
  • Be the first to comment

Top ten of PHP 7.4

  1. 1. Top ten of php 7.4 Andrea Giannantonio Team Leader in Immobiliare.it @JellyBellyDev
  2. 2. RELEASE DATE PHP 7.4 •Alpha1 - 13 Jun 2019 •Beta1 - 25 Jul 2019 •RC1 - 5 Sep 2019 •RC2 - 19 Sep 2019 •RC3 - 03 Oct 2019 •RC4 - 18 Oct 2019 •RC5 - 31 Oct 2019 •RC6 - 14 Nov 2019 •7.4.0 - 28 Nov 2019 (GA) •7.4.1 - 18 Dec 2019 (GA) •7.4.2 - 23 Jan 2020 (GA)
  3. 3. TOP TEN •Typed Properties •Arrow Functions •Null Coalescing Assignment Operator •Spread Operator in Array •Foreign Function Interface •Weak References •Covariant Returns and Contravariant Parameters •Preloading •Deprecate left-associative ternary operator •Performance of PHP 7.4
  4. 4. ONE Typed Properties
  5. 5. PARAM AND RETURN TYPES: PHP 7.1
  6. 6. Allowed: bool, int, float, string, array, object, iterable, self, parent, any class or interface name, and nullable types (?type) Not Allowed: void and callable TYPED PROPERTIES: PHP 7.4
  7. 7. TYPED PROPERTIES: NULL
  8. 8. TYPED PROPERTIES: STRICT_TYPES OFF
  9. 9. TYPED PROPERTIES: INVARIANT Property types (public and protected) are invariant
  10. 10. TWO Arrow Functions
  11. 11. ARROW FUNCTIONS
  12. 12. ARROW FUNCTIONS
  13. 13. ARROW FUNCTIONS Syntax: fn(parameter_list) => expr Note: fn is a reserved word in PHP 7.4
  14. 14. THREE Spread Operator in Array
  15. 15. SPREAD OPERATOR Note that this only works with arrays with numerical keys.
  16. 16. SPREAD OPERATOR It also works on Traversable objects
  17. 17. FOUR Null Coalescing Assignment Operator
  18. 18. NULL COALESCING ASSIGNMENT OPERATOR Note that, while the coalesce operator `??` is a comparison operator, `??=` is an assignment operator.
  19. 19. FIVE Foreign Function Interface
  20. 20. FOREIGN FUNCTION INTERFACE •Enables the execution of C code directly from PHP •Provides a FFI class to manage the bridge between C and PHP •A C function can be executed as a PHP method of FFI class •php.ini: `ffi.enable=false`
  21. 21. FFI: EXAMPLE 1
  22. 22. FFI: EXAMPLE 2
  23. 23. FFI: PHP TENSORFLOW Dmitry Stogov has developed an experimental binding of TensorFlow library in PHP using FFI: dstogov/php-tensorflow
  24. 24. SIX Weak References
  25. 25. WEAK REFERENCES 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. 
  26. 26. SEVEN Covariant Returns and Contravariant Parameters
  27. 27. COVARIANT RETURNS AND CONTRAVARIANT PARAMETERS Variance is a property of class hierarchies describing how the types of a type constructor affect subtypes. •Invariant: if the type of the super-type constrain the type of the subtype. •Covariant: if the ordering of types is preserved (types are ordered from more specific to more generic). •Contravariant: if it reverses the order (types are ordered from more generic to more specific). Currently, PHP has mostly invariant parameter and return types, with few exceptions. This RFC proposes to allow covariance and contravariance on parameter types and return types. In general, a type constructor can be:
  28. 28. COVARIANT RETURN TYPE: EXAMPLE
  29. 29. CONTRAVARIANT PARAMETER TYPE: EXAMPLE
  30. 30. EIGHT Preloading
  31. 31. OPCACHE
  32. 32. PRELOADING "On server startup – before any application code is run – we may load a certain set of PHP files into memory – and make their contents “permanently available” to all subsequent requests that will be served by that server. All the functions and classes defined in these files will be available to requests out of the box, exactly like internal entities." Dmitry Stogov
  33. 33. PRELOADING • Preloading is controlled by a specific php.ini directive: opcache.preload • This directive specifies a PHP script to be compiled and executed at server start-up • Each file to cache, should be added in this script using opcache_compile_file()
  34. 34. PRELOADING: COMPOSITION opcache.preload=“/tmp/preload.php” OPCache Preloading in Symfony opcache.preload=/path/to/project/var/cache/prod/App_KernelProdContainer.preload.php
  35. 35. PRELOADING: BENCHMARKS • Preloading disabled • Naive preloading - all Laravel and application code I found this benchmark by stitcher.io on Laravel project • Preloading Optimised - dumped all classes used by `get_declared_classes()` Scenarios:
  36. 36. PRELOADING: BENCHMARKS Requests per second Time per request That's around a 25% performance gain compared to not using preloading, and an 8% gain compared to using the naive approach.
  37. 37. PRELOADING: CONS • Restart the web server to update opcache.preload • Not compatible with host running multiple applications, or multiple versions of applications
  38. 38. NINE Deprecate left-associative ternary operator
  39. 39. DEPRECATE LEFT-ASSOCIATIVE TERNARY OPERATOR Unlike most other languages, the ternary operator in PHP is left-associative rather than right-associative. Not only being uncommon, it is also confusing for programmers who switch between different languages. PHP 7.4 proposes to remove the left-associativity and requires the use of parentheses instead. • Starting from PHP 7.4, nested ternaries without explicit use of parentheses will throw a deprecation warning. • Starting from PHP 8.0, there will be a compile runtime error.
  40. 40. TEN Performance of PHP 7.4
  41. 41. PERFORMANCE OF PHP 7.4 • He compared the execution time of PHP7.4RC1 with PHP 7.1, 7.2 and 7.3 • He used Zendbench.php script as benchmark I found this benchmark on PHP 7.X by Enrico Zimuel • He took the total execution time • Full experiment report here
  42. 42. PERFORMANCE OF PHP 7.4: RESULTS PHP ver. Exec. time (in sec) 7.1.32 455 7.2.22 437 7.3.9 373 7.4RC1 334
  43. 43. PERFORMANCE OF PHP 7.4: RESULTS PHP 7.4 IS THE FASTEST! • 7.4RC1 is 26% faster than 7.1.32 • 7.4RC1 is 22% faster than 7.2.22 • 7.4RC1 is 10% faster than 7.3.9
  44. 44. LET’S GO CODING IN PHP 7.4
  45. 45. Thanks for the attention Andrea Giannantonio @JellyBellyDev
  46. 46. REFERENCES • https://www.php.net • https://wiki.php.net/rfc#php_74 • https://www.zimuel.it/slides/pugtorino/php74#/ • https://www.zimuel.it/blog/benchmarking_PHP74 • https://medium.com/@daniel.dan/whats-new-in-php-7-4-top-10-features-that-you-need-to-know-e0acc3191f0a • https://kinsta.com/blog/php-7-4/ • https://stitcher.io/blog/new-in-php-74 • https://stitcher.io/blog/php-preload-benchmarks • https://support.cloud.engineyard.com/hc/en-us/articles/205411888-PHP-Performance-I-Everything-You-Need-to- Know-About-OpCode-Caches • https://carbon.now.sh/

×