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

What to expect from PHP 7

What to expect from PHP 7

Presentation for PHP East Midlands user group on the upcoming features in PHP 7, how to upgrade and how to get involved

Lorna Mitchell

August 06, 2015
Tweet

More Decks by Lorna Mitchell

Other Decks in Technology

Transcript

  1. What To Expect From
    PHP 7
    Lorna Mitchell

    View Slide

  2. About PHP 7
    PHP 7 is the next major release of PHP
    • Follows PHP 5.6 (there is no PHP 6)
    • Currently in beta
    • Due for final release in November 2015
    • First major release of PHP since 2004

    View Slide

  3. PHP 7 Is Fast

    View Slide

  4. PHP 7 Is Fast

    View Slide

  5. Why PHP 7 Is Fast
    • Grew from the phpng project
    • Influenced by HHVM/Hacklang
    • Major refactoring of the Zend Engine
    • More compact data structures throughout
    • As a result all extensions need updates
    • http://gophp7.org/php7-ext
    Rasmus' stats: http://talks.php.net/fluent15#/6

    View Slide

  6. New Features

    View Slide

  7. Combined Comparison Operator
    The <=> "spaceship" operator is for quick greater/less
    than comparison.
    1 echo 2 <=> 1; // 1
    2 echo 2 <=> 3; // -1
    3 echo 2 <=> 2; // 0
    4
    Use it with numbers, strings and even arrays - but not
    objects.

    View Slide

  8. Ternary Shorthand
    Refresher on this PHP 5 feature:
    1 echo $count ? $count : 10; // 10
    2 echo $count ?: 10; // 10
    3

    View Slide

  9. Null Coalesce Operator
    Operator ?? is ternary shorthand (?:) but with isset().
    1 $b = 16;
    2
    3 echo $a ?? 2; // 2
    4 echo $a ?? $b ?? 7; // 16
    5

    View Slide

  10. Type Hints
    PHP 5 has type hinting, allowing you to say what kind of
    parameter is acceptable in a method call.
    1 function myfunc(array $list, $length) {
    2 return array_slice($list, 0, $length);
    3 }
    4

    View Slide

  11. Type Hints
    If we use the wrong parameter types, it errors
    1 print_r(myfunc(3, 3));
    2
    PHP 5 error:
    PHP Catchable fatal error: Argument 1 passed to myfunc() must be of the
    type array, integer given
    PHP 7 error:
    Fatal error: Uncaught TypeError: Argument 1 passed to myfunc() must be of
    the type array, integer given

    View Slide

  12. Scalar Type Hints
    PHP 7 lets us hint more datatypes:
    • string
    • int
    • float
    • bool

    View Slide

  13. Scalar Type Hints
    We can amend our code accordingly:
    1 function myfunc(array $list, int $length) {
    2 return array_slice($list, 0, $length);
    3 }
    4
    And then call it:
    1 $moves = ['hop', 'skip', 'jump', 'tumble'];
    2 print_r(myfunc($moves, "2")); // ['hop', 'skip']
    3

    View Slide

  14. Scalar Type Hints
    By default, type hints are coercive, so string "2" is
    acceptable for an int hint.
    To enable strict type check, add this line in the calling
    context:
    declare(strict_types=1);
    With this line in place, our string argument will fail the
    type hint.

    View Slide

  15. Return Type Hints
    We can also type hint for return values. This is awesome.
    1 function myfunc(array $list, int $length): array {
    2 if($length > 0) {
    3 return array_slice($list, 0, $length);
    4 }
    5 return false;
    6 }
    7
    Beware that we can't return false or null.

    View Slide

  16. Return Type Hints
    1 $moves = ['hop', 'skip', 'jump', 'tumble'];
    2 print_r(myfunc($moves, "2")); // ['hop', 'skip']
    3
    The above works, the below does not:
    1 $moves = ['hop', 'skip', 'jump', 'tumble'];
    2 print_r(myfunc($moves, 0));
    3
    Fatal error: Uncaught TypeError: Return value of myfunc() must be of the
    type array, boolean returned

    View Slide

  17. Exceptions and Errors
    PHP 5 exceptions are alive, well, and awesome.

    View Slide

  18. Exceptions in PHP 7
    Very familiar but they now inherit from Throwable.

    View Slide

  19. Throwable Interface
    One of a small number of predefined interfaces in PHP.
    Describes most of the Exception functionality, so that
    the Error class behaves the same way.

    View Slide

  20. Errors in PHP 7
    Some errors are now catchable via the Error class.

    View Slide

  21. Error Class
    The Error looks like Exception but won't be caught by
    existing catch blocks. This is good for upgrading.
    An Error is something the programmer did wrong in
    the first place, and an Exception is something
    unexpected that happened along the path of execution.
    You can catch both with stacked catch blocks.

    View Slide

  22. Catching Exceptions and Errors
    1 function somethingFun(int $count, array $list) {
    2 throw new Exception("You fail");
    3 }
    4 try {
    5 $a = somethingFun(1,1);
    6 } catch (Exception $e) {
    7 echo "you hit the exception line";
    8 } catch (TypeError $e) {
    9 echo "you passed the wrong arguments"; }
    10

    View Slide

  23. Catch Method Calls on Non-Objects
    Does this error look familiar?
    1 $a->grow();
    2
    PHP 5:
    PHP Fatal error: Call to a member function grow() on null
    PHP 7:
    Fatal error: Uncaught Error: Call to a member function grow() on unknown

    View Slide

  24. Catch Method Calls on Non-Objects
    1 try {
    2 $a->grow();
    3 } catch (Error $e) {
    4 echo "(oops! " . $e->getMessage() . ")\n";
    5 }
    6

    View Slide

  25. Upgrading to PHP 7

    View Slide

  26. Upgrading to PHP 7
    Step 1: Upgrade to PHP 5.5 or 5.6.

    View Slide

  27. Upgrading to PHP 7
    Step 1: Upgrade to PHP 5.5 or 5.6.
    Most PHP 5 code will just work with a few pitfalls to look
    out for.
    I expect all modern applications to be upgradeable (and
    therefore upgraded!).

    View Slide

  28. Uniform Variable Syntax
    This is a feature as well as a gotcha.
    • Good news: more consistent and complete variable
    syntax with fast parsing
    • Bad news: some quite subtle changes from old syntax
    when dereferencing or using $$
    • If in doubt, add more { and }
    RFC: https://wiki.php.net/rfc/uniform_variable_syntax
    Static analyser: https://github.com/rlerdorf/phan

    View Slide

  29. Foreach
    Check that you're not relying on any foreach()
    weirdnesses
    • The array pointer will no longer move, look out for
    use of current() and next() inside a foreach()
    loop
    • Don't assign to the thing you're looping over, the
    behaviour has changed
    RFC: https://wiki.php.net/rfc/php7_foreach

    View Slide

  30. Deprecated Features
    The majority of things that trigger E_DEPRECATED in
    older versions of PHP are now actually removed.
    This includes the mysql_* functions. PDO is great, I
    promise.

    View Slide

  31. Upgrading to PHP 7
    There are fabulous comprehensive instructions
    https://github.com/php/php-src/blob/php-7.0.0beta2/
    UPGRADING
    To make the business case:
    • calculate hardware cost saving
    • calculate developer time required
    Done :)

    View Slide

  32. PHP 7 and You

    View Slide

  33. PHP 7 and You
    How smoothly will the launch go if nobody tests it?

    View Slide

  34. Use PHP 7
    • Use Rasmus' dev box
    https://github.com/rlerdorf/php7dev
    • Compile new PHP yourself
    https://github.com/php/php-src/
    • Use Zend's nightly builds for your platform
    http://php7.zend.com/
    • Bitnami LAMP/MAMP stacks
    http://lrnja.net/1M5fEnH

    View Slide

  35. How To Test
    Get PHP 7 and then:
    • Run your test suites (travis already has PHP 7
    available)
    • Then run your actual PHP 5 applications
    • Narrow down good replication cases, report bugs to
    appropriate place
    Tutorial for putting your project onto php7dev:
    http://lrnja.net/1MSlFkt

    View Slide

  36. PHP 7 Is Coming
    It's fast and ... fabulous

    View Slide

  37. Questions?
    Slides are on http://lornajane.net
    (related blog posts are there too)
    Contact me
    [email protected]
    • @lornajane

    View Slide

  38. Bonus Slides

    View Slide

  39. Multiple Import Declarations
    Syntactic sugar perhaps, but very readable code. Start
    with:
    1 use Symfony\Component\Form\Form;
    2 use Symfony\Component\Form\FormError;
    3 use Talk\TalkDb;
    4 use Talk\TalkApi;
    5 use User\UserDb;
    6 use User\UserApi;
    7

    View Slide

  40. Multiple Import Declarations
    Syntactic sugar perhaps, but very readable code. Now
    reads:
    1 use Symfony\Component\Form\{Form, FormError};
    2 use Talk\{TalkDb, TalkApi};
    3 use User\{UserDb, UserApi};
    4
    Group your imports, also supports aliases.

    View Slide

  41. No E_STRICT
    Replaced with either E_DEPRECATED or E_NOTICE or
    E_WARNING
    Simplifies error stuff in PHP 7

    View Slide

  42. Anonymous Classes
    Start with this (normal) class:
    4 class Logger {
    5 public function log($message) {
    6 echo $message;
    7 }
    8 }
    9
    10 $log1 = new Logger();
    11

    View Slide

  43. Anonymous Classes
    Now consider this anonymous class:
    13 $log2 = new class extends Logger {
    14 public function log($message) {
    15 echo $message . "\n";
    16 }
    17 };
    18

    View Slide

  44. Anonymous Classes
    Compare the two in use:
    21 $log1->log("one line");
    22 $log1->log("another line");
    23 echo "----\n";
    24 $log2->log("one line");
    25 $log2->log("another line");
    26
    one lineanother line----
    one line
    another line

    View Slide

  45. Hex Numbers in Strings
    PHP 7 doesn't detect hex numbers when casting strings
    to numeric values.

    View Slide