Skip to content

Instantly share code, notes, and snippets.

@byevhen2
Last active October 16, 2025 19:32
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.

PHP 7.4

All features.

  • Null coalescing assignment operator: $array['key'] ??= 'initial value'.
  • Typed properties: private 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.

PHP 8.0

All features.

  • mixed pseudo-type: function parse_int(mixed $input) (in addition to bool, int, float, string, array, object, iterable, callable, self, parent and interface/class name).
  • static return type: public static function get_instance(): static {}.
  • Named arguments: parse_int($input, max: 10).
  • Union types: function validate_int(mixed $input): int|false.
    • But not false, false|null or ?false prior to PHP 8.2 (see the caution).
  • Constructor property promotion (declaring properties in the constructor signature):
    class Point
    {
        public function __construct(private int $x, private int $y = 0) {}
    }
  • Nullsafe operator (?->): get_booking(31)?->get_price() ?? 0.0.
  • Support for Attributes has been added.
  • Posibility to fetch the class name of an object using $object::class. The result is the same as get_class($object).
  • Added Stringable interface.
    • Automatically implemented if a class defines a __toString() method.
  • Traits can now define abstract private methods (must be implemented by the class using the trait).
  • throw can be used as an expression: $user = $session->user ?? throw new Exception('Must have user.');.
  • An optional trailing comma is allowed in parameter lists:
    function f(
        $parameter1,
        $parameter2, // This comma is now allowed
    )
  • It is possible to write catch (Exception) without storing it in a variable.
  • New methods/functions:

PHP 8.1

  • never return-only type indicating the function does not terminate: calls exit(), throws an exception, or is an infinite loop.
    • Cannot be part of a union type declaration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment