Skip to content

Instantly share code, notes, and snippets.

@byevhen2
Last active February 22, 2022 21:01
Show Gist options
  • Save byevhen2/53969979bb1c0f04afc8f4587880eee9 to your computer and use it in GitHub Desktop.
Save byevhen2/53969979bb1c0f04afc8f4587880eee9 to your computer and use it in GitHub Desktop.
PHP: New Features

PHP 7.0

All features.

  • Scalar type declarations: function f(int ...$args) (bool, int, float and string, in addition to array, callable, self, parent and interface/class name).
  • Return type declarations: function f(): bool.
  • Constant arrays using define(): define('MY_CONSTANT', [ ... ]).
  • Integer division with intdiv(): var_dump(intdiv(8, 3)); // int(3).
  • Spaceship operator: $a <=> $b → {-1, 0, 1} (less, equal, bigger).
  • Null coalescing operator:
    // $username = isset($_GET['user']) && !is_null($_GET['user']) ? $_GET['user'] : 'nobody';
    $username = $_GET['user'] ?? 'nobody';
    
    $username = $_GET['user'] ?? $_POST['user'] ?? 'nobody';
  • Group use declarations: use some\namespace\{ClassA, ClassB, ClassC as C}.
  • Generator return expressions.
  • Generator delegation.
  • Anonymous classes.

PHP 7.1

All features.

  • iterable pseudo-type: function f(iterable $i) (accepts arrays and Traversable's).
  • Nullable types: function f(?string $arg): ?int.
  • Void functions: function f(): void.
  • Multi catch exception handling:
    try {
        ...
    } catch (Exception1 | Exception2 $e) { ... }
  • Class constant visibility:
    class MyClass
    {
        const           PUBLIC_CONSTANT_1 = 1;
        public const    PUBLIC_CONSTANT_2 = 2;
        protected const PROTECTED_CONSTANT = 4;
        private const   PRIVATE_CONSTANT = 8;
    }
  • Symmetric array destructuring:
    $data = [
        [1, 'Tom'],
        [2, 'Fred'],
    ];
    
    ...
    
    [$id, $name] = $data[0]; // list($id, $name) = $data[0];
    
    foreach ($data as [$id, $name]) { // foreach ($data as list($id, $name))
        ...
    }
  • Support for keys in list():
    $data = [
        ['id' => 1, 'name' => 'Tom'],
        ['id' => 2, 'name' => 'Fred'],
    ];
    
    list('id' => $id, 'name' => $name) = $data[0];
    
    foreach ($data as list('id' => $id, 'name' => $name)) {
        ...
    }

PHP 7.2

All features.

  • object pseudo-type: function f(object $o) (in addition to bool, int, float, string, array, iterable, callable, self, parent and interface/class name (and mixed since PHP 8.0)).
  • Abstract method overriding:
    abstract class A
    {
        abstract public function f(string $x);
    }
    
    abstract class B
    {
        // Maintaining contravariance for parameters and covariance for return
        abstract public function f($x): int;
    }

PHP 7.3

All features.

  • New functions: array_key_first(), array_key_last(), is_countable().
  • Array destructuring supports reference assignments: [&$id, &$name] = $data[0].

PHP 7.4

All features.

  • Null coalescing assignment operator: $array['key'] ??= 'initial value'.
  • Typed properties: public int $id.
  • Arrow functions: fn($n) => $n * $factor, where $factor is a parent scope variable.
  • Unpacking inside arrays: $fruits = ['banana', 'orange', ...$otherFruits].
  • Numeric literal separator: 299_792_458; // Decimal number.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment