Is my code ready for PHP 7 ?
I told engineering
to upgrade all servers
to PHP 7. That's OK,
right ? I uh...
I guess...
Before we begin
Send a text to +32460200098
Powered by PHP 7
Copyright Michelangelo ;-)
Who am I ?
Wim Godden (@wimgtr)
Founder of Cu.be Solutions (http://cu.be)
Open Source developer since 1997
Developer of PHPCompatibility, OpenX, PHPConsistent, ...
Speaker at Open Source conferences
Why vs How
Why upgrade ?
Bad reasons :
It's cool to have the latest version
Annoy sysadmins
Oh cool, a new toy !
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
The numbers
W3Techs (http://w3techs.com/technologies/details/pl-php/all/all)
Dec 2015 Jan 2015 Nov 2013
PHP 4 : 1.3% 1.8% 2.7%
PHP 5 : 98.7% 98.2% 97.3%
5.0 : <0.1% < 0.1% 0.1%
5.1 : 0.9% 1.2% 2.0%
5.2 : 13.2% 19.2% 37.6%
5.3 : 35.9% 45.5% 52.0%
5.4 : 31.0% 26.9% 7.9%
5.5 : 14.5% 6.3% 0.5%
5.6 : 4.4% 0.5% < 0.1%
PHP 7 : < 0.1%
So...
Should you upgrade today ?
Upgrade : yes / no
Yes No
Using removed extensions x
Using removed functions x
Need extra performance / reduced memory x
Really need new feature x
Want to use recent framework x
No unit tests x
No package available (.rpm, .deb, ...) x
Postponing upgrades
End-Of-Life
In the past : we'll see
Now : minor release + 2 = out → EOL
5.6 = OUT → 5.4 = EOL
7.0 = OUT → 5.5 = EOL
Critical security patches : 1 year
No bugfixes
Framework support
Developer motivation
So you want to upgrade...
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
Option 3 : automated static analysis
Unit tests on different PHP versions
Vagrant boxes
Integrate into your CI
Use Travis CI (http://travis-ci.org)
Why make it so hard ? A best-case scenario...
Development environment : 5.6
Production environment : 5.6
All is well, right ?
Today : PHP 7 arrives
How can you test compatibility ?
→ Set up your test environment today
→ Even for new projects
Migrating an entire infrastructure ?
Back in 2010...
PHP Architect @ Belgian Railways
8 years of legacy code
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
PEAR package (pear install PHP_CodeSniffer)
Composer (squizlabs/php_codesniffer)
Detect 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
PHP_CodeSniffer
Let's see what it looks like
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 and above
PHPCompatibility – making it work
Via Composer : wimg/php-compatibility
From Github :
Download :
http://github.com/wimg/PHPCompatibility
Install in <pear_dir>/PHP/CodeSniffer/Standards
Run :
phpcs --standard=PHPCompatibility <path>
PHPCompatibility
Let's see what it looks like
Important notes
Large directories → can be slow !
Use --extensions=php,phtml
No point scanning .js files
Static analysis
Doesn't run code
Can not detect every single incompatibility
Provides filename and line number
You can check for specific versions :
--runtime-set testVersion 5.3
or
--runtime-set testVersion 5.3-5.5
The result
Zend Framework 1.7 app
PHP 5.2 : working fine
PHP 5.3 : fail !
function goto()
Some common apps – phpBB (latest)
FILE: /usr/src/phpBB3/adm/index.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 2 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
48 | WARNING | INI directive 'safe_mode' is deprecated in PHP 5.3 and forbidden in PHP 5.4.
48 | WARNING | INI directive 'safe_mode' is deprecated in PHP 5.3 and forbidden in PHP 5.4.
--------------------------------------------------------------------------------
Common apps - MediaWiki
FILE: /usr/src/mediawiki-1.19.8/includes/GlobalFunctions.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
2705 | WARNING | The use of function dl is discouraged in PHP version 5.3 and
| | discouraged in PHP version 5.4 and discouraged in PHP version
| | 5.5
--------------------------------------------------------------------------------
Common apps – Wordpress (latest)
FILE: /usr/src/wordpress/wp-admin/includes/class-pclzip.php
--------------------------------------------------------------------------------
FOUND 2 ERROR(S) AFFECTING 2 LINE(S)
--------------------------------------------------------------------------------
5340 | ERROR | The use of function set_magic_quotes_runtime is discouraged in
| | PHP version 5.3 and forbidden in PHP version 5.4 and forbidden
| | in PHP version 5.5
5371 | ERROR | The use of function set_magic_quotes_runtime is discouraged in
| | PHP version 5.3 and forbidden in PHP version 5.4 and forbidden
| | in PHP version 5.5
--------------------------------------------------------------------------------
FILE: /usr/src/wordpress/wp-includes/SimplePie/Item.php
--------------------------------------------------------------------------------
FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
125 | WARNING | INI directive 'zend.ze1_compatibility_mode' is deprecated in
| | PHP 5.3 and forbidden in PHP 5.4.
--------------------------------------------------------------------------------
FILE: /usr/src/wordpress/wp-includes/wp-db.php
--------------------------------------------------------------------------------
FOUND 16 ERROR(S) AFFECTING 16 LINE(S)
--------------------------------------------------------------------------------
641 | ERROR | Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli
| | instead.
646 | ERROR | Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli
| | instead.
PHP 7 rules status
Under development
Lots of work !
Changes for PHP 7 – deprecated functionality
PHP4 style constructors
Static calls to non-static methods
password_hash salt option
Changes for PHP 7 – new functionality
scalar type declaration
return type declaration
null coalescing operator
spaceship operator
constant arrays in define
anonymous classes
unserialize filter variable
IntlChar class
Group use declaration
Generator return expression
Generator delegation
intdiv function
session_start options
preg_replace_callback_array function
random_bytes and random_int functions
Changes for PHP 7 – backwards incompatibility
Variable handling interpretation
Empty list assignments
Array ordering when using by reference assignment
global keyword with variable variables no longer allowed
Function parenthesis warning
foreach internal array pointer changes
foreach by-value and by-reference changes
Negative bitshifts
Removed functions call_user_method, call_user_method_array, mcrypt_generic_end,
mcrypt_ecb(), mcrypt_cbc, mcrypt_cfb, mcrypt_ofb, datefmt_set_timezone_id,
IntlDateFormatter::setTimeZoneID, set_magic_quotes_runtime,
magic_quotes_runtime, set_socket_blocking, imagepsbbox, imagepsencodefont,
imagepsextendfont, imagepsfreefont, imagepsloadfont, imagepsslantfont,
imagepstext
Onwards to page 2...
Changes for PHP 7 – backwards incompatibility
Removed INI directives always_populate_raw_post_data, asp_tags and
xsl.security_prefs
New objects assigned by reference removed
New reserved keywords bool, int, float, string, NULL, TRUE, FALSE, resource,
object, mixed and numeric
Functions with multiple parameters with same name not allowed
Switch statements with multiple defaults not allowed
$HTTP_RAW_POST_DATA removed
# comments in INI files removed (only tested for php.ini)
mktime and gmmktime no longer support is_dst parameter
preg_replace no longer supports e
setlocale no longer supports category as string
Changes for PHP 7 – backwards incompatibility
Several new built-in functions, classes, interfaces and
exceptions
Many new global constants
Many removed extensions
Loosening reserved word restrictions (in some places)
PHP 7 rules status
Should be completed by end of December
Might be sponsored :-)
Conclusion
No 100% detection
But : 95% automation = lots of time saved !
First : PHPCompatibility on local machine
Then : use your CI/test environment and run unit tests
Start upgrading !
Questions ?
Questions ?
We're hiring !
Looking for a challenge ?
Want to do more than just code all day ?
→ http://cu.be/jobs
Thanks !

Is your code ready for PHP 7 ?

  • 1.
    Is my codeready for PHP 7 ? I told engineering to upgrade all servers to PHP 7. That's OK, right ? I uh... I guess...
  • 2.
    Before we begin Senda text to +32460200098 Powered by PHP 7 Copyright Michelangelo ;-)
  • 3.
    Who am I? Wim Godden (@wimgtr) Founder of Cu.be Solutions (http://cu.be) Open Source developer since 1997 Developer of PHPCompatibility, OpenX, PHPConsistent, ... Speaker at Open Source conferences
  • 4.
    Why vs How Whyupgrade ? Bad reasons : It's cool to have the latest version Annoy sysadmins Oh cool, a new toy ! How to upgrade ? The nightmare of compatibility The joy of automation No miracles here !
  • 5.
    Show of hands 3/ 4 5.0 5.1 5.2 5.3 5.4 5.5 5.6 6.0 7.0
  • 6.
    The numbers W3Techs (http://w3techs.com/technologies/details/pl-php/all/all) Dec2015 Jan 2015 Nov 2013 PHP 4 : 1.3% 1.8% 2.7% PHP 5 : 98.7% 98.2% 97.3% 5.0 : <0.1% < 0.1% 0.1% 5.1 : 0.9% 1.2% 2.0% 5.2 : 13.2% 19.2% 37.6% 5.3 : 35.9% 45.5% 52.0% 5.4 : 31.0% 26.9% 7.9% 5.5 : 14.5% 6.3% 0.5% 5.6 : 4.4% 0.5% < 0.1% PHP 7 : < 0.1%
  • 7.
  • 8.
    Upgrade : yes/ no Yes No Using removed extensions x Using removed functions x Need extra performance / reduced memory x Really need new feature x Want to use recent framework x No unit tests x No package available (.rpm, .deb, ...) x
  • 9.
    Postponing upgrades End-Of-Life In thepast : we'll see Now : minor release + 2 = out → EOL 5.6 = OUT → 5.4 = EOL 7.0 = OUT → 5.5 = EOL Critical security patches : 1 year No bugfixes Framework support Developer motivation
  • 10.
    So you wantto upgrade...
  • 11.
    So you wantto 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 Option 3 : automated static analysis
  • 12.
    Unit tests ondifferent PHP versions Vagrant boxes Integrate into your CI Use Travis CI (http://travis-ci.org)
  • 13.
    Why make itso hard ? A best-case scenario... Development environment : 5.6 Production environment : 5.6 All is well, right ? Today : PHP 7 arrives How can you test compatibility ? → Set up your test environment today → Even for new projects
  • 14.
    Migrating an entireinfrastructure ?
  • 15.
    Back in 2010... PHPArchitect @ Belgian Railways 8 years of legacy code 40+ different developers 40+ projects Challenge : migrate all projects from PHP 5.1.x (on Solaris) to PHP 5.3.x (on Linux)
  • 16.
    The idea Automate it How? → Use the CI environment Which tool ? → PHP_CodeSniffer
  • 17.
    PHP_CodeSniffer PEAR package (pearinstall PHP_CodeSniffer) Composer (squizlabs/php_codesniffer) Detect 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
  • 18.
  • 19.
    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 and above
  • 20.
    PHPCompatibility – makingit work Via Composer : wimg/php-compatibility From Github : Download : http://github.com/wimg/PHPCompatibility Install in <pear_dir>/PHP/CodeSniffer/Standards Run : phpcs --standard=PHPCompatibility <path>
  • 21.
  • 22.
    Important notes Large directories→ can be slow ! Use --extensions=php,phtml No point scanning .js files Static analysis Doesn't run code Can not detect every single incompatibility Provides filename and line number You can check for specific versions : --runtime-set testVersion 5.3 or --runtime-set testVersion 5.3-5.5
  • 23.
    The result Zend Framework1.7 app PHP 5.2 : working fine PHP 5.3 : fail ! function goto()
  • 24.
    Some common apps– phpBB (latest) FILE: /usr/src/phpBB3/adm/index.php -------------------------------------------------------------------------------- FOUND 0 ERROR(S) AND 2 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 48 | WARNING | INI directive 'safe_mode' is deprecated in PHP 5.3 and forbidden in PHP 5.4. 48 | WARNING | INI directive 'safe_mode' is deprecated in PHP 5.3 and forbidden in PHP 5.4. --------------------------------------------------------------------------------
  • 25.
    Common apps -MediaWiki FILE: /usr/src/mediawiki-1.19.8/includes/GlobalFunctions.php -------------------------------------------------------------------------------- FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 2705 | WARNING | The use of function dl is discouraged in PHP version 5.3 and | | discouraged in PHP version 5.4 and discouraged in PHP version | | 5.5 --------------------------------------------------------------------------------
  • 26.
    Common apps –Wordpress (latest) FILE: /usr/src/wordpress/wp-admin/includes/class-pclzip.php -------------------------------------------------------------------------------- FOUND 2 ERROR(S) AFFECTING 2 LINE(S) -------------------------------------------------------------------------------- 5340 | ERROR | The use of function set_magic_quotes_runtime is discouraged in | | PHP version 5.3 and forbidden in PHP version 5.4 and forbidden | | in PHP version 5.5 5371 | ERROR | The use of function set_magic_quotes_runtime is discouraged in | | PHP version 5.3 and forbidden in PHP version 5.4 and forbidden | | in PHP version 5.5 -------------------------------------------------------------------------------- FILE: /usr/src/wordpress/wp-includes/SimplePie/Item.php -------------------------------------------------------------------------------- FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S) -------------------------------------------------------------------------------- 125 | WARNING | INI directive 'zend.ze1_compatibility_mode' is deprecated in | | PHP 5.3 and forbidden in PHP 5.4. -------------------------------------------------------------------------------- FILE: /usr/src/wordpress/wp-includes/wp-db.php -------------------------------------------------------------------------------- FOUND 16 ERROR(S) AFFECTING 16 LINE(S) -------------------------------------------------------------------------------- 641 | ERROR | Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli | | instead. 646 | ERROR | Extension 'mysql_' is deprecated since PHP 5.5 - use mysqli | | instead.
  • 27.
    PHP 7 rulesstatus Under development Lots of work !
  • 28.
    Changes for PHP7 – deprecated functionality PHP4 style constructors Static calls to non-static methods password_hash salt option
  • 29.
    Changes for PHP7 – new functionality scalar type declaration return type declaration null coalescing operator spaceship operator constant arrays in define anonymous classes unserialize filter variable IntlChar class Group use declaration Generator return expression Generator delegation intdiv function session_start options preg_replace_callback_array function random_bytes and random_int functions
  • 30.
    Changes for PHP7 – backwards incompatibility Variable handling interpretation Empty list assignments Array ordering when using by reference assignment global keyword with variable variables no longer allowed Function parenthesis warning foreach internal array pointer changes foreach by-value and by-reference changes Negative bitshifts Removed functions call_user_method, call_user_method_array, mcrypt_generic_end, mcrypt_ecb(), mcrypt_cbc, mcrypt_cfb, mcrypt_ofb, datefmt_set_timezone_id, IntlDateFormatter::setTimeZoneID, set_magic_quotes_runtime, magic_quotes_runtime, set_socket_blocking, imagepsbbox, imagepsencodefont, imagepsextendfont, imagepsfreefont, imagepsloadfont, imagepsslantfont, imagepstext Onwards to page 2...
  • 31.
    Changes for PHP7 – backwards incompatibility Removed INI directives always_populate_raw_post_data, asp_tags and xsl.security_prefs New objects assigned by reference removed New reserved keywords bool, int, float, string, NULL, TRUE, FALSE, resource, object, mixed and numeric Functions with multiple parameters with same name not allowed Switch statements with multiple defaults not allowed $HTTP_RAW_POST_DATA removed # comments in INI files removed (only tested for php.ini) mktime and gmmktime no longer support is_dst parameter preg_replace no longer supports e setlocale no longer supports category as string
  • 32.
    Changes for PHP7 – backwards incompatibility Several new built-in functions, classes, interfaces and exceptions Many new global constants Many removed extensions Loosening reserved word restrictions (in some places)
  • 33.
    PHP 7 rulesstatus Should be completed by end of December Might be sponsored :-)
  • 34.
    Conclusion No 100% detection But: 95% automation = lots of time saved ! First : PHPCompatibility on local machine Then : use your CI/test environment and run unit tests Start upgrading !
  • 35.
  • 36.
  • 37.
    We're hiring ! Lookingfor a challenge ? Want to do more than just code all day ? → http://cu.be/jobs
  • 38.

Editor's Notes

  • #5 part 2 look at difficulties you might encounter in upgrading. I&amp;apos;ll provide solutions not a magician can&amp;apos;t solve everything ;-)
  • #7 5.3.3 = Debian Squeezy = 12% Wait a second... that means people aren&amp;apos;t even on 5.3 ? And there was 3 year gap between the release of 5.2 and 5.3, so 5.3 brought a lot of cool things.