Diese Präsentation wurde erfolgreich gemeldet.
Die SlideShare-Präsentation wird heruntergeladen. ×

The why and how of moving to php 8

Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Anzeige
Wird geladen in …3
×

Hier ansehen

1 von 69 Anzeige

The why and how of moving to php 8

Herunterladen, um offline zu lesen

With PHP 8.0 recently released and PHP 5.x still accounting for over 40% of all production environments, it's time to paint a clear picture on not just why everyone should move to 8.x, but on how to get code ready for the latest version of PHP. In this talk, we'll look at some handy tools and techniques to ease the migration.

With PHP 8.0 recently released and PHP 5.x still accounting for over 40% of all production environments, it's time to paint a clear picture on not just why everyone should move to 8.x, but on how to get code ready for the latest version of PHP. In this talk, we'll look at some handy tools and techniques to ease the migration.

Anzeige
Anzeige

Weitere Verwandte Inhalte

Diashows für Sie (20)

Ähnlich wie The why and how of moving to php 8 (20)

Anzeige

Weitere von Wim Godden (20)

Aktuellste (20)

Anzeige

The why and how of moving to php 8

  1. 1. The Why and How of moving to PHP 8
  2. 2. Who am I ? Wim Godden (@wimgtr)
  3. 3. Where I'm from
  4. 4. Where I'm from
  5. 5. Where I'm from
  6. 6. My town
  7. 7. My town
  8. 8. Who am I ? Wim Godden (@wimgtr) Founder of Cu.be Solutions (https://cu.be) Open Source developer since 1997 Developer of PHPCompatibility, OpenX, ... Speaker at Open Source conferences
  9. 9. Why vs How Part 1 : why upgrade ? Bad reasons : Oh cool, a new toy ! Annoy sysadmins Part 2 : how to upgrade ? The nightmare of compatibility The joy of automation No miracles here !
  10. 10. Show of hands 3 / 4 5.0 5.1 5.2 5.3 5.4 5.5 5.6 6.0 7.0 7.1 7.2 7.3 7.4 8.0
  11. 11. The numbers W3Techs (http://w3techs.com/technologies/details/pl-php/all/all) Now Oct 2018 Jan 2015 PHP 4 : 0.3% 0.8% 1.8% PHP 5 : 41.6% 87.2% 98.2% 5.0 : < 0.1% < 0.1% 0.1% 5.1 : 0.3% 0.5% 1.2% 5.2 : 6.3% 7.8% 19.2% 5.3 : 14.8% 19.7% 45.5% 5.4 : 16.3% 21.2% 26.9% 5.5 : 9.5% 15.4% 6.3% 5.6 : 52.8% 35.3% 0.5% PHP 7 : 58.0% 12.0% 7.0 : 11.9% 66.8% 7.1 : 11.5% 31.2% 7.2 : 31.5% 2.1% 7.3 : 28.2% 7.4 : 16.9%
  12. 12. 5.3 – 5.6 quick recap Namespaces () Late static binding Closures Better garbage collection Goto Mysqlnd Performance gain Short array syntax Traits Built-in webserver Binary notation No more register_globals, magic_quotes_gpc and safe_mode Generators (yield keyword) password_hash() function Built-in opcache
  13. 13. PHP 7.x quick recap Performance increase Lower memory consumption Scalar type and return type declaration (with nullable types since 7.1) Null coalaescing operator (??) and null coalesce assignment operator (??=) Spaceship operator (<=>) CSPRNG functions for random data generation Improved error handling (most of them now catchable) FFI for direct communication with C libraries Abstract syntax tree Improved consistency throughout Lots of things removed or deprecated
  14. 14. PHP 8 Deprecations from PHP 7 are turned into removed functionality New features Syntax and functionality changes Deprecations that will be removed in PHP 9
  15. 15. Deprecated in PHP 7.0, removed in PHP 8 (1/3) PHP 4 style constructors Static calls to non-static methods
  16. 16. Deprecated in PHP 7.0, removed in PHP 8 (2/3) create_function() is removed __autoload() is removed each() is removed
  17. 17. Deprecated in PHP 7.0, removed in PHP 8 (3/3) Operator precedence deprecation : In PHP 7.4 : Deprecated: The behavior of unparenthesized expressions containing both '.' and '+'/'-' will change in PHP 8: '+'/'-' will take a higher precedence in test.php on line 4 In PHP 8 : Output : “Sum is : 8” Deprecated curly brace syntax for accessing array elements : Output : “3” !!! Removed
  18. 18. JIT (Just In Time) Compilation Detects ‘hot’ parts of the code Basically parts that are accessed a lot Tries to optimize these parts Huge gain on mathematical calculations Limited gain on typical web application code So… useless ? Depends… PHP is used for many different things nowadays, so definitely a worthwhile addition !
  19. 19. Union types Not valid : Valid :
  20. 20. Named arguments
  21. 21. Named arguments
  22. 22. Named arguments
  23. 23. Nullsafe operator ( ?-> ) Before PHP 8 : PHP 8 :
  24. 24. Match expression
  25. 25. Attributes Also known as annotations Adds metadata to a functins, classes, etc, without docblocks Can be fetched programmatically Syntax :
  26. 26. Attributes - larger example
  27. 27. Attributes - using reflection Retrieving information about the attributes (on a class in this case) :
  28. 28. Attributes My/Attribute/ExampleAttribute.php : Program code :
  29. 29. Constructor property promotion
  30. 30. Constructor property promotion Conflict, not allowed
  31. 31. Constructor property promotion
  32. 32. Mixed type When you really don’t know in advance what type you’re going to need As parameter : As return type :
  33. 33. Non-capturing catches PHP < 8 required parameter PHP 8 doesn’t anymore
  34. 34. :class as an alternative for get_class() Pre-PHP 8 PHP 8
  35. 35. New string functions Since the early days of PHP : Since PHP 8 :
  36. 36. New string functions : str_starts_with() and str_ends_with()
  37. 37. Abstract methods in traits - change Pre-PHP8 PHP 8
  38. 38. Abstract methods in extended classes - change Conflicts Pre-PHP 8 : Warning: Declaration of … should be compatible with ... PHP 8 : Fatal Error: Declaration of … should be compatible with ...
  39. 39. Internal functions have type annotations
  40. 40. TypeErrors on internal functions User-defined functions already threw TypeError Internal functions will follow same behaviour, throwing TypeError or ValueError
  41. 41. ValueError Pre-PHP 8 PHP 8
  42. 42. Many warnings are now catchable errors and 2 more pages...
  43. 43. Default error reporting level Previously : E_NOTICE and E_DEPRECATED Now : E_ALL This means errors that never showed before might not show up
  44. 44. Strict type checks Pre-PHP8 PHP 8 : TypeError will be thrown
  45. 45. Some other changes JSON is always available PDO : default error mode is now : exceptions @ no longer silences fatal errors display_startup_errors is on by default crypt() function requires $salt parameter Assertions throw an exception instead of a warning (by default) More details : http://php.watch https://stitcher.io/blog/new-in-php-8
  46. 46. So... Should you upgrade today ?
  47. 47. Postponing upgrades - End-Of-Life In the past : we'll see Now : 2 years of bugfixes after initial release 1 extra year of critical security patches In practice 7.1 was released Dec 2016 → already EOL 7.2 was released Nov 2017 → already EOL (Nov 2019) 7.3 was released Dec 2018 → EOL Dec 2020 7.4 was released Nov 28 → EOL Nov 2021 8.0 will be released on Nov 26 → EOL Nov 2022 If you’re on PHP 7.0 - 7.3 → start upgrading !
  48. 48. Postponing upgrades Security Performance Framework support Symfony 5 : PHP 7.2.5+ Laravel 6 : PHP 7.2+ Developer motivation
  49. 49. Upgrade paths 1 upgrade every 5 years Knowledge of upgrade steps will be forgotten Documentation is not very useful (for example : SysvInit → Systemd) Massive task to upgrade all apps, all environments, all servers Upgrade every release Upgrade steps can be automated Can be integrated with continuous integration and continuous deployment Documentation is in the automation flow
  50. 50. So you want to upgrade... Option 1 : run your unit tests Option 2 : visit each page (good luck !) + check error_log Or : record visits, then replay log on test environment Or : proxy requests to 2 environments Option 3 : automated static analysis
  51. 51. Back in 2010... PHP Architect @ Belgian Railways 8 years of legacy code (4.x and 5.x) 40+ different developers 40+ projects Challenge : migrate all projects from PHP 5.1.x (on Solaris) to PHP 5.3.x (on Linux)
  52. 52. The idea Automate it How ? → Use the CI environment Which tool ? → PHP_CodeSniffer
  53. 53. PHP_CodeSniffer Originally PEAR package (pear install PHP_CodeSniffer) Also on Composer now Detects coding standard violations Supports multiple standards Static analysis tool → Runs without executing code → Splits code in tokens Ex. : T_OPEN_CURLY_BRACKET T_FALSE T_SEMICOLON → Parses each file separately
  54. 54. PHP_CodeSniffer Let's see what it looks like
  55. 55. PHP_CodeSniffer options -i Show available standards -p Show progress -s Show real error/warning sniff names -n Ignore warnings -v Verbose --parallel=x (since PHP_CodeSniffer 3)
  56. 56. PHPCompatibility PHP_CodeSniffer standard Only purpose : find compatibility issues Detects : Deprecated functions Deprecated extensions Deprecated php.ini settings and ini_set() calls Prohibited function names, class names, … … Works for PHP 5.0 5.3 and above (5.4 for PHP_CodeSniffer 3 support)
  57. 57. PHPCompatibility – making it work - Composer In require-dev : phpcompatibility/php-compatibility If PHPCompatibility is the only PHP CodeSniffer standard : "scripts": { "post-install-cmd": ""vendor/bin/phpcs" --config-set installed_paths vendor/phpcompatibility/php-compatibility/PHPCompatibility", "post-update-cmd" : ""vendor/bin/phpcs" --config-set installed_paths vendor/phpcompatibility/php-compatibility/PHPCompatibility" } Otherwise use one of these : DealerDirect/phpcodesniffer-composer-installer higidi/composer-phpcodesniffer-standards-plugin
  58. 58. PHPCompatibility – making it work – testing and running Check if coding standard is available : phpcs -i Should output something similar to : The installed coding standards are MySource, PEAR, PHPCompatibility, PHPCS, PSR1, PSR2, Squiz and Zend To run : phpcs --standard=PHPCompatibility /path/of/your/code
  59. 59. Important notes Large directories → can be slow ! Use --extensions=php No point scanning .js files Test PHP x.x compatibility → needs PHP x.x on the system Static analysis Doesn't actually run the code Can not detect every single incompatibility → some things only happen on runtime Provides filename and line number
  60. 60. Checking for specific versions Default : latest PHP version Check for single version : phpcs --standard=PHPCompatibility --runtime-set testVersion 8.0 srcdir Check for multiple specific versions : phpcs --standard=PHPCompatibility --runtime-set testVersion 7.0-7.1 srcdir Check for minimum version : phpcs --standard=PHPCompatibility --runtime-set testVersion 7.0- srcdir Checking for older version : phpcs --standard=PHPCompatibility --runtime-set testVersion 5.0 srcdir
  61. 61. Extra rulesets
  62. 62. Other tools For Wordpress : PHP Compatibility Checker (uses PHPCompatibility)
  63. 63. Other tools For Wordpress : PHP Compatibility Checker (uses PHPCompatibility) PhpStorm 10+ : PHP 7 Compatibility Inspection sstalle/php7cc : similar functionality, slightly less up-to-date exakat/exakat : general quality inspection phan/phan : general static analyzer, compatibility checks are mostly useful for type checking adamculp/php-compatibility-check : docker image that uses PHPCompatibility, php7cc and phan
  64. 64. Conclusion No 100% detection But : 95% automation = lots of time saved ! First : PHPCompatibility on local machine Then : use your CI environment Start upgrading !
  65. 65. Big thanks to... Juliette Reinders Folmer Has been the main contributor (95%+ of all commits) in last 4-5 years PHP_CodeSniffer wizard
  66. 66. Questions ?
  67. 67. Questions ?
  68. 68. Thanks !

×