The New New PHP

Posted on Tue, 5/10/2016
3 minutes read

PHP 7, it's new, released last fall and makes life better. PHP 6 never happened it was just PHP 5.3. This talk was by Larry Garfield of Platform.sh

What is new?

One of the most important releases in a decade, biggest change since PHP 5. You can view the slides to this presentation on his website

  1. Return Types
    • As of PHP 7 we have return types.
    • not nullable (good)
      • helps with error handling because Type system will handle weird edge cases
    • Returning false on error is a big 'screw you' to your users
  2. Type Specs
    • full type list with new new type specs
      • int *new
      • float *new
      • string *new
      • bool *new
      • array
      • callable
      • any class name
    • random zip codes need to be strings because some start with 0 and integers cannot start with 0
    • Strict vs Weak mode
      • in Weak by default
        • string -> int may E_NOTICE
      • Strict
        • ```php

        declare(strict_types=1);

  class Address inplements AddressInterface {
    public function _construct(string $street, string $city, string $state) {
      // ...
    }
    public function getZip() :string {
      // ...
    }
  }
  ```
  - Int convers to float automatically if needed.
  - When should you use strict types
    - 90% of the time
    - (everywhere except input)
      - on input everything is a string to start
  - Also, Documentation
    - Stict typing makes it so you don't need too much documentation
  1. T_SPACESHIP <=>
    • Does cool stuff
  2. NULL coalesce!
    • $username = $username ?? 'Anyonmous';
      • returns username is it is defined & not null.
      • used to be: $username = isset($username) && !is_null($username) ?: 'Anyonymous';
  3. CSPRNG! (Crypotgraphically Secure Psedudo-...)
    • Cryptography is hard!
    • rand() isn't actually random - DONT USE THIS FOR CRYPTOGRAPHY!
    • mctypt() don't use that either
    • openssl is hard to use
    • $junk = random_bytes(16);
    • $val = random)int(1, 100);
    • Only use random_byes() random_int()
  4. Fakes
    • Mocking is hard
    • Fakes are easy
  5. Type Errors
    • Fatal errors Suck!
    • Recoverable errors... aren't actually recoverable
    • You can actually catch type errors

    -

     try {
       // ...
     }
     catch (\ParseError $e) {
       // ...
     }
    
    • Exceptions: the user screwed up
    • Errors: the coder screwed up
  6. Expectations
    • Assertions suck
    • it makes assert not suck
  7. Generators
  8. Uniform varialbe syntax
    • Always Left-To-Right

    - ```php $foo()'bar'; [$obj1, $ob2][0]->prop; getStr(){0} $foo['bar']::$baz;

(...)['foo'];
(...)->foo;

(function() { ... })();
($obj->closure)();
```

PHP 7 is really really fast! How did it do that? Too nerdy... basically the PHP engine rewrote most of the PHP engine to be faster.

Drupal on PHP 7 is WAYY faster than Wordpress like 3+X faster! You can see 50% improvement in speed just by switching to PHP 7. If you take away nothing else from this switching to PHP 7 is the best performance boost you can do. PHP 7 is the most tested .0 release of PHP ever, it's safe.

PHP 7-ready hosts: 34 listed on phpversions.info/php-7 meaning you can run Drupal 8 on PHP 7 today!

PHP 7 is the correct way to run Drupal 8