SlideShare a Scribd company logo
The Why and How of moving to PHP 8
Who am I ?
Wim Godden (@wimgtr)
Where I'm from
Where I'm from
Where I'm from
My town
My town
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
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 !
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
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%
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
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
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
Deprecated in PHP 7.0, removed in PHP 8 (1/3)
PHP 4 style constructors
Static calls to non-static methods
Deprecated in PHP 7.0, removed in PHP 8 (2/3)
create_function() is removed
__autoload() is removed
each() is removed
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
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 !
Union types
Not valid :
Valid :
Named arguments
Named arguments
Named arguments
Nullsafe operator ( ?-> )
Before PHP 8 :
PHP 8 :
Match expression
Attributes
Also known as annotations
Adds metadata to a functins, classes, etc, without docblocks
Can be fetched programmatically
Syntax :
Attributes - larger example
Attributes - using reflection
Retrieving information about the attributes (on a class in this case) :
Attributes
My/Attribute/ExampleAttribute.php :
Program code :
Constructor property promotion
Constructor property promotion
Conflict, not allowed
Constructor property promotion
Mixed type
When you really don’t know in advance what type you’re going to need
As parameter :
As return type :
Non-capturing catches
PHP < 8 required parameter
PHP 8 doesn’t anymore
:class as an alternative for get_class()
Pre-PHP 8
PHP 8
New string functions
Since the early days of PHP :
Since PHP 8 :
New string functions : str_starts_with() and str_ends_with()
Abstract methods in traits - change
Pre-PHP8
PHP 8
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 ...
Internal functions have type annotations
The why and how of moving to php 8
TypeErrors on internal functions
User-defined functions already threw TypeError
Internal functions will follow same behaviour, throwing TypeError or ValueError
ValueError
Pre-PHP 8
PHP 8
Many warnings are now catchable errors
and 2 more pages...
Default error reporting level
Previously : E_NOTICE and E_DEPRECATED
Now : E_ALL
This means errors that never showed before might not show up
Strict type checks
Pre-PHP8
PHP 8 : TypeError will be thrown
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
So...
Should you upgrade today ?
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 !
Postponing upgrades
Security
Performance
Framework support
Symfony 5 : PHP 7.2.5+
Laravel 6 : PHP 7.2+
Developer motivation
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
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
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)
The idea
Automate it
How ? → Use the CI environment
Which tool ? → PHP_CodeSniffer
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
PHP_CodeSniffer
Let's see what it looks like
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)
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)
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
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
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
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
Extra rulesets
Other tools
For Wordpress : PHP Compatibility Checker (uses PHPCompatibility)
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
Conclusion
No 100% detection
But : 95% automation = lots of time saved !
First : PHPCompatibility on local machine
Then : use your CI environment
Start upgrading !
Big thanks to...
Juliette Reinders Folmer
Has been the main contributor (95%+ of all commits) in last 4-5 years
PHP_CodeSniffer wizard
Questions ?
Questions ?
Thanks !

More Related Content

The why and how of moving to php 8

  • 1. The Why and How of moving to PHP 8
  • 2. Who am I ? Wim Godden (@wimgtr)
  • 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. 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. 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. 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. 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. 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. 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. Deprecated in PHP 7.0, removed in PHP 8 (1/3) PHP 4 style constructors Static calls to non-static methods
  • 16. Deprecated in PHP 7.0, removed in PHP 8 (2/3) create_function() is removed __autoload() is removed each() is removed
  • 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. 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 !
  • 23. Nullsafe operator ( ?-> ) Before PHP 8 : PHP 8 :
  • 25. Attributes Also known as annotations Adds metadata to a functins, classes, etc, without docblocks Can be fetched programmatically Syntax :
  • 27. Attributes - using reflection Retrieving information about the attributes (on a class in this case) :
  • 32. Mixed type When you really don’t know in advance what type you’re going to need As parameter : As return type :
  • 33. Non-capturing catches PHP < 8 required parameter PHP 8 doesn’t anymore
  • 34. :class as an alternative for get_class() Pre-PHP 8 PHP 8
  • 35. New string functions Since the early days of PHP : Since PHP 8 :
  • 36. New string functions : str_starts_with() and str_ends_with()
  • 37. Abstract methods in traits - change Pre-PHP8 PHP 8
  • 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. Internal functions have type annotations
  • 41. TypeErrors on internal functions User-defined functions already threw TypeError Internal functions will follow same behaviour, throwing TypeError or ValueError
  • 43. Many warnings are now catchable errors and 2 more pages...
  • 44. Default error reporting level Previously : E_NOTICE and E_DEPRECATED Now : E_ALL This means errors that never showed before might not show up
  • 45. Strict type checks Pre-PHP8 PHP 8 : TypeError will be thrown
  • 46. 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
  • 48. 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 !
  • 49. Postponing upgrades Security Performance Framework support Symfony 5 : PHP 7.2.5+ Laravel 6 : PHP 7.2+ Developer motivation
  • 50. 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
  • 51. 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
  • 52. 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)
  • 53. The idea Automate it How ? → Use the CI environment Which tool ? → PHP_CodeSniffer
  • 54. 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
  • 56. 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)
  • 57. 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)
  • 58. 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
  • 59. 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
  • 60. 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
  • 61. 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
  • 63. Other tools For Wordpress : PHP Compatibility Checker (uses PHPCompatibility)
  • 64. 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
  • 65. Conclusion No 100% detection But : 95% automation = lots of time saved ! First : PHPCompatibility on local machine Then : use your CI environment Start upgrading !
  • 66. Big thanks to... Juliette Reinders Folmer Has been the main contributor (95%+ of all commits) in last 4-5 years PHP_CodeSniffer wizard