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

PHP 7.x - Past, Present Feature

PHP 7.x - Past, Present Feature

We talked about some interesting things that came to php since the release of php 7. Covered the new features in php 7.1 in more detail and took a look at what is known already for php 7.2.

PHP Varna User Group, March 2017

Boyan Yordanov

March 16, 2017
Tweet

More Decks by Boyan Yordanov

Other Decks in Programming

Transcript

  1. PHP 7.X - PAST, PRESENT, FUTURE
    Boyan Yordanov

    View full-size slide

  2. ABOUT ME
    Developer at Shtrak
    Community fanboy
    Wannabe speaker and writer
    Helping make this happen

    View full-size slide

  3. WHERE TO FIND ME?
    @specter_bg
    boyanyordanov
    boyanyordanov

    View full-size slide

  4. PHP 7 TIMELINE
    The Past: PHP 5.6 -> PHP 7.0
    The Present: PHP 7.0 -> PHP 7.1
    The Future: PHP Next (7.2)

    View full-size slide

  5. DEATH OF PHP 5
    VERSION SUPPORT

    View full-size slide

  6. 5.6 IS PRACTICALLY ON LIFE SUPPORT
    AND PROJECTS ARE DROPPING IT
    The force is weak with those who think that “*” is a
    version constraint. twitter.com/tobySen/status…
    7:27 PM ­ 5 Feb 2017 · Oberding, Deutschland
    21 43
    Sebastian Bergmann
    @s_bergmann
    Follow
    Laravel 5.5 in July 2017 will require PHP 7+
    4:29 PM ­ 16 Dec 2016
    419 810
    Taylor Otwell
    @taylorotwell
    Follow
    #Symfony 4 (to released in November 2017) will drop
    #PHP 5 support... So, #Twig 2 stable should probably
    supports PHP 7 only, right?
    2:54 PM ­ 16 Dec 2016
    264 383
    Fabien Potencier
    @fabpot
    Follow
    For the future 2.6 release I will drop support for PHP <
    7.0 to massively simplify code. Should I keep support
    for PHP 5?
    1:22 PM ­ 13 Dec 2016 · City of London, London
    39 12
    Xdebug
    @xdebug
    Follow

    View full-size slide

  7. PHP 7
    Worldwide celebration

    View full-size slide

  8. Speed and Memory consumption
    Abstract Syntax Trees (AST)

    View full-size slide

  9. Anonymous classes
    Cheap stubs
    $stub = new class implements SomeInterface{
    public function methodStub() {}

    $subject = new ClassIamTesting($stub);

    View full-size slide

  10. Returning objects
    function objFromArray() {
    return (object) [
    'prop' => 'value';

    }
    // ...
    function objFromAnnonymous() {
    retrurn new class {
    public $prop = 'value';

    }

    View full-size slide

  11. Return types
    Scalar type hints
    Throwable and Error
    and more
    declare(strict_types=1)

    View full-size slide

  12. MORE INFO
    Black re.io - optimizations added in PHP 7
    How Badoo saved one million dollars switching to
    PHP7

    View full-size slide

  13. PHP 7.1
    New stuff

    View full-size slide

  14. NULLABLE TYPES
    Previously
    function dummy(int $a) : int {
    return $a;
    }
    dummy(null); // TypeError

    View full-size slide

  15. NULLABLE TYPES
    Accepts null, but the argument is optional
    function dummy(int $a = null) : int {
    return $a ?? 0;
    }
    dummy(null); // returns 0
    dummy(); // still returns 0

    View full-size slide

  16. NULLABLE TYPES
    Now - allow null, but require int
    function dummy(?int $a) : ?int {
    return $a;
    }
    dummy(null); // returns null

    View full-size slide

  17. NULLABLE TYPES
    A more real use case
    function sum(?int $a, ?int $b) : int {
    $a = $a ?? 0;
    $b = $b ?? 0;
    return $a + $b;
    }
    ...
    // web request / console arguments
    $a = getFromInput('a');
    $b= getFromInput('b');
    $sum = sum($a, $b);

    View full-size slide

  18. VOID RETURN TYPE
    function noResult() : void {
    // Do stuff
    }

    View full-size slide

  19. VOID RETURN TYPE
    function noResult() : void {
    return 1; // Fatal error
    }

    View full-size slide

  20. VOID RETURN TYPE
    But you can still `return` to break execution
    function noResult() : void {
    // Do stuff
    if($someContition) {
    return;
    }
    // Do more stuff
    }

    View full-size slide

  21. ITERABLE PSEUDO-TYPE
    function iterateStruct(iterable $array) {
    foreach($array as $item) {
    // Do stuff with $item
    }
    }

    View full-size slide

  22. ITERABLE PSEUDO-TYPE
    Same result with any construct you can iterate over
    $array = ['item 1', 'item 2', 'item 3'];
    $collection = new class implements Iterator { ... };
    function gen() {
    yield 'item 1';
    yield 'item 2';
    yield 'item 3';
    }
    iterateStruct($array);
    iterateStruct($collection);
    iterateStruct(gen());

    View full-size slide

  23. LIST() AND ARRAY DESTRUCTURING CHANGES
    Use keys with list()
    Short syntax
    list('foo' => $foo, 'baz' =>$baz) = ['foo' => 1, 'bar' => 2];
    [$foo, $bar] = [1, 2, 3];
    ['foo' => $foo] = ['foo' => 1, 'bar' => 2];

    View full-size slide

  24. CLASS CONSTANT VISIBILITY
    Declare private, protected and public constants
    class SpecialConfig {
    private const MY_VALUE = 'value';
    public function doStuff() {
    echo self::MY_VALUE;
    }
    }
    $special = new SpecialConfig();
    $special­>doStuff(); // prints 'value'
    echo SpecialConfig::MY_VALUE; // throws error

    View full-size slide

  25. CATCHING MULTIPLE EXCEPTIONS
    try {
    // Do stuff and throw exception
    } catch (OneException | TwoException $e) {
    // add some common handling code
    }

    View full-size slide

  26. CATCHING MULTIPLE EXCEPTIONS
    Without the new thingy
    try {
    // Do stuff and throw exception
    } catch (OneException $e) {
    // do stuff with $e
    } catch (TwoException $e) {
    // copy­paste the same code from above
    }

    View full-size slide

  27. CLOSURE::FROMCALLABLE()
    Create closures from callable pseudo-type
    // Without the new method
    array_map(function($item) {}, $arr);
    array_map('myCallback', $arr);
    // With the new syntax
    array_map(Closure::fromCallable('myCallback'), $arr);
    // Throws Error
    array_map(Closure::fromCallable('myCallback_wrong_name'), $arr);

    View full-size slide

  28. CLOSURE::FROMCALLABLE()
    You can use class methods too
    $arr = ['item 1', 'item 2', 'item 3'];
    class MyClass {
    public function methodCallback($item) {
    echo $item;
    }
    }
    $myCallback = Closure::fromCallable([new MyClass, 'methodCallback'
    echo get_class($myCallback); // Prints Closure
    array_map($myCallback, $arr);

    View full-size slide

  29. ARGUMENTCOUNTERROR
    NEGATIVE STRING OFFSETS
    function countsOnArguments($arg) {}
    countsOnArguments(); // throws ArgumentCountError
    $phpVarna = 'PHP Varna is cool.'
    echo $phpVarna[­5]; // prints "c"

    View full-size slide

  30. PHP NEXT
    CURRENTLY PHP 7.2

    View full-size slide

  31. WHAT DO WE KNOW?
    No release date yet - probably in December again
    A bunch of things are deprecated
    New password hashing algorithm in core
    Libsodium will be in core

    View full-size slide

  32. LIBSODIUM
    From the RFC
    Libsodium is a modern cryptography library that offers
    authenticated encryption, high-speed elliptic curve
    cryptography, and much more. Unlike other cryptography
    standards (which are a potluck of cryptography primitives;
    i.e. WebCrypto), libsodium is comprised of carefully
    selected algorithms implemented by security experts to
    avoid side-channel vulnerabilities.

    View full-size slide

  33. MODERN CRYPTOGRAPHY YES - NAMESPACES - HELL NO!
    #PHP 7.2 will be the first programming language to accept
    modern cryptography into its standard
    library.news.php.net/php.internals/…
    11:09 PM ­ 10 Feb 2017
    451 494
    Scott Arciszewski
    @CiPHPerCoder
    Follow
    But no namespaces. God help you if we find namespaces.
    twitter.com/ciphpercoder/s…
    2:10 AM ­ 11 Feb 2017
    25
    SaraMG
    @SaraMG
    Follow

    View full-size slide

  34. ARGON2 HASHING ALGORITHM
    Uses PASSWORD_ARGON2I in password_* functions
    Control the parameters
    password_hash('password', PASSWORD_ARGON2I);
    passowrd_hash('password', PASSWORD_ARGON2I, [
    'memory_cost' => PASSWORD_ARGON2_DEFAULT_MEMORY_COST,
    'time_cost' => PASSWORD_ARGON2_DEFAULT_TIME_COST,
    'threads' => PASSWORD_ARGON2_DEFAULT_THREADS
    ]);

    View full-size slide

  35. PARAMETER TYPE WIDENING
    Skip type hints when overriding parent class methods
    class ArrayClass {
    public function foo(array $foo) { /* ... */ }
    }
    // This RFC proposes allowing the type
    // to be widened to be untyped aka any
    // type can be passed as the parameter.
    // Any type restrictions can be done
    // via user code in the method body.
    class EverythingClass extends ArrayClass {
    public function foo($foo) { /* ... */ }
    }

    View full-size slide

  36. COUNTING OF NON-COUNTABLE OBJECTS
    Any scalar, null or object not implementing Countable
    will raise a Warning
    CONVERT NUMERIC KEYS IN OBJECT/ARRAY CASTS
    Numeric keys are no longer lost in the conversion
    $obj = (object)['1' => 1, '2' => 2, '3' => 3];

    View full-size slide

  37. HOW TO STAY ON TOP?
    You don't have to

    View full-size slide

  38. NAVIGATING THE INTERNALS NEWS
    1. RFCs - the best source, sometimes abandoned or
    postponed a lot
    2. The mailing list - old, chaotic, hard to follow
    3. - internals mailing list in a nice UI
    4. Twitter: and
    many more, because the community is awesome!
    5. Blogs and
    6.
    Externals.io
    @of cial_php @nikita_ppv @SaraMG
    PHP Weekly newsletter
    PRs on PHP-SRC repo on GitHub

    View full-size slide

  39. QUESTIONS?
    THANK YOU!

    View full-size slide