Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Essential PHP 7

Essential PHP 7

Presented on May 18th 2016 at NijmegenPHP, Nijmegen, The Netherlands.
http://www.meetup.com/NijmegenPHP/
---------------------------------------------------------------
Overview of the most important changes introduced with PHP 7.
We've all heard that PHP 7 is fabulous: faster than any previous PHP version and even HHVM, some nifty new functionality, and much better exception management. But what do we need to do to make our code compatible with PHP 7? What will break and how do we avoid this?

More Decks by Juliette Reinders Folmer

Other Decks in Programming

Transcript

  1. What Can You Expect ? New features! Less BC breaks

    than upgrading to 5.4! PHPNG (PHP Next-Gen) Speed !
  2. So Long And Thanks For All The Fish MySql extension

    Ereg Posix Regex PHP 5.4 and lower
  3. • dl() on fpm-fcgi • set_magic_quotes_runtime() • set_socket_blocking() • mcrypt_generic_end()

    • mcrypt_ecb() • mcrypt_cnc() • mcrypt_cfb() • datefmt_set_timezone_id • IntlDateFormatter::setTimeZoneID • call_user_method() • call_user_method_array() Removed Functions
  4. • xsl.security_prefs • Input encoding: – iconv.input_encoding – Iconv.internal_encoding –

    mbstring.http_input – mbstring.http_output – mbstring.internal_encoding Removed ini Options
  5. What Will Break ? preg_replace /e Static vs $this new

    ... by reference Multiple default: Division by zero Numeric Hex strings Some Variable Variables Alternative PHP tags
  6. What Will Break ? func_get_ args() Reserved words PHP4 construc-

    tors $HTTP_ RAW_ POST_ DATA Invalid octal literals list() vs string access Internal array pointer vs foreach() Redefining function args
  7. Goodies! Spaceship Unicode Escapes Strict Typing More Exception Types More

    Catchable Errors Uniform Variable Syntax Null Coalesce Scalar Type Hints & Return Type Hints
  8. Uniform Variable Syntax Syntax Old Interpretation New Interpretation $$var[‘key1’][‘key2’] ${$var[‘key1’][‘key2’]}

    ($$var)[‘key1’][‘key2’] $var->$prop[‘key’] $var->{$prop[‘key’]} ($var->$prop)[‘key’] $var->$prop[‘key’]() $var->{$prop[‘key’]}() ($var->$prop)[‘key’]() Class::$var[‘key’]() Class::{$var[‘key’]}() (Class::$var)[‘key’]()
  9. Combined Comparison Operator <?php // Pre PHP 7 function sort_order(

    $a, $b ) { return ( $a < $b ) ? -1 : ( ( $a > $b ) ? 1 : 0 ); } // PHP7+ function sort_order( $a, $b ) { return $a <=> $b; }
  10. Null Coalesce Operator // Pre PHP 7 $post = (

    ( isset( $post ) ? $post : ‘default’ ); // PHP7+ $post = $post ?? ‘default’; // PHP 7.1+ $post ??= ‘default’;
  11. Scalar Type Hints & Strict Typing <?php declare( strict_types =

    1 ); // must be first line function get_post_type( int $post_id ) { // do something; return $post_type; } try { get_post_type( $post_object ); } catch ( \TypeException $e ) { var_dump( $e ); }
  12. Return Type Hints <?php function is_post( int $post_id ) :

    bool { // do something return ( ‘post’ === $post->post_type ); }
  13. More Goodies! Session options Preg_ replace_ callback_ array() intdiv() Expectations

    Closure call() Filtered unserialize() Exceptions on Fatals IntlChar Class
  14. PHP5.3+ on Steriods Bind Closure on Call Group Use Declarations

    Anonymous Classes Generator Enhancements Assertions
  15. • Are the PHP Extensions Ready ? – http://gophp7.org/ –

    https://github.com/gophp7/gophp7-ext/wiki/extensions- catalog • PHP 7 Compatibility Checker – https://github.com/sstalle/php7cc • PHP Compatibility – https://github.com/wimg/PHPCompatibility/ • Need a Dev Box ? – https://github.com/rlerdorf/php7dev • PHP 7 to PHP 5.6 transphiler – https://github.com/jaytaph/Transphpile Useful