Top PHP 8 Features You Don't Know About

PHP 8 has been officially released on November 26, 2020. There are number of features that came out along with php 8 release.

Followings are some of the features that we will discuss about today:

  • PHP JIT (Just in Time Compiler)
  • Constructor Property Promotion
  • Validation for Abstract Trait Methods
  • Incompatible Method Signatures
  • Arrays Starting With a Negative Index
  • Union Types 2.0
  • Consistent Type Errors for Internal Functions
  • throw Expression
  • Weak Maps
  • Trailing Comma in Parameter List
  • Allow ::class syntax on objects
  • Attributes v2
  • Named Arguments
  • Nullsafe Operator
  • Saner String to Number Comparisons
  • Saner Numeric Strings
  • Match Expression v2
  • Stricter Type Checks for Arithmetic/Bitwise Operators

PHP JIT (Just in Time Compiler)

To understand what is JIT first of all we need to understand basics of PHP. How PHP interpreter converts PHP code and output as a text on browser or console.

Please read about my previous article on how php works click below link to read before we talk about JIT.

How PHP Works?

PHP script goes through following process to parse php code to machine language:

  • When you run php script the Zend Engine will parse the code into an abstract syntax tree (AST).
  • Interpreter then translates AST nodes into low-level Zend opcodes
  • Later opcodes are translated to machine language

Before PHP 8, using OpCache extension php code is translated to opcode faster by caching opcode so that it does not have to go through lexing and compilation stages.

A just-in-time (JIT) compiler will take the output of the opcodes without interpreting them, it will compile them into machine code and invoke that object code instead.

This is done in memory therefore it is much faster.

Constructor Property Promotion

Consider following code before PHP 8:

<?php

class Shape {
    public int $height;
    public int $width;

    public function __construct(
        int $height = 0,
        int $width = 0
    ) {
        $this->width = $width;
        $this->height = $height;
    }
}

Above code can be replaced with following php 8 feature.

<?php

class Shape {
    public function __construct(
        public int $height = 0,
        public int $width = 0
    ) { }
}

Validation for Abstract Trait Methods

Traits in PHP are re-usable code block, it supports abstracts methods as well. You can now write abstract method in trait to impose validation on class using traits.

Consider following code:

<?php

trait ShapeTrait {
	abstract public function area(int $height, int $width);
}

class Shape {
    use ShapeTrait;

    // Allowed, but shouldn't be due to invalid type.
	public function area(string $height, string $width) {}
}

Above code will throw fatal error during compilation process because of invalid parameter type declaration.

Incompatible Method Signatures

If a class is implementing an interface, incompatible method signatures throw a fatal error:

<?php

interface Shape {
	abstract public function area(int $height, int $width);
}

class Circle implements Shape {
	public function area(string $height, string $width) {}
}

Arrays Starting With a Negative Index

In PHP 7.4 consider following code:

<?php

// array index starts with -5 then will go to 0, 1, 2 and so on
var_dump( array_fill(-5, 4, true) );

// output
array(4) {
	[-5]=>
	bool(true)
	[0]=>
	bool(true)
	[1]=>
	bool(true)
	[2]=>
	bool(true)
}

In PHP8 above code will output as:

<?php

// array index starts with -5 then will go to 0, 1, 2 and so on
var_dump( array_fill(-5, 4, true) );

// output
array(4) {
	[-5]=>
	bool(true)
	[-4]=>
	bool(true)
	[-3]=>
	bool(true)
	[-2]=>
	bool(true)
}

Reference: https://kinsta.com/blog/php-8/#abstract-trait-methods