What is new in PHP 8

Last modified date

Comments: 0

What is new in PHP 8

PHP 8 is to be released on November 26, 2020. It is a major version of the PHP, which comes with some breaking changes from existing version of php. In this post what is new in the PHP 8.

Due to some of the breaking changes, you need to make changes in your existing code to run it on PHP 8. Some of the methods, functions are deprecated too.

here are the list of new features to be used on PHP 8.

1. Union types :

Union types are collection of two or more types, which indicates that one of them can be used.

public function foo(Foo|Bar $input): int|float

Note that void can not be part of union type as it indicates null return type. furthermore nullable values can be written using ? sign.

public function foo(?Bar $input) : void

2. JIT

The Just In Time compiler. As you know php is an interpreted language. It is not compiled like Java, C. It compiles the code at runtime instead. So JIT is a technique which compiles the part of code at runtime, so that compile part of the code to be used instead.

Think of it like it is a cached version of the code which is interpreted.

3. Named Arguments

Named arguments are passed the values into function just by specifying the name of the arguments so that we do not need to remember the order of the argument to be passed.

function calculate(string $type, int $a, ?int $b){
/** function definition*/
}

sum(type:'sum',a:5,b:2);

4. Match Expressions

It is just like switch statement. You can call it big brother of switch statement. match can return values, and does not require the break statement. It can combine conditions and use strict type for comparisons.

$result = match($a){
0 => "hello"
'1','2','3' => "world"
}

5. Create Value Objects (data transfer objects)

PHP 8 supports the value objects in the constructor method in php classes. it also called constructor property promotion.

class Money 
{
    public Currency $currency;
 
    public int $amount;
 
    public function __construct(
        Currency $currency,
        int $amount,
    ) {
        $this->currency = $currency;
        $this->amount = $amount;
    }
}

6. Arrays starting with negative index

In PHP, if array starts with negative index, the next index would be start with 0.

$array = array_fill(-5,4,true);
var_dump($array);
It would result as ---

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

In php 8, the result would be different as 

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

7. Attributes

These are commonly known as annotations in other language like java. It offers a way to add meta data to classes without having to parse doc blocks.

#[Attribute]
class ExampleAttribute
{
    public $value;
 
    public function __construct($value)
    {
        $this->value = $value;
    }
}

//attribute to be used in Foo class
use App\Attributes\ExampleAttribute;

#[ExampleAttribute]
class Foo
{
    #[ExampleAttribute]
    public const FOO = 'foo';
 
    #[ExampleAttribute]
    public $x;
 
    #[ExampleAttribute]
    public function foo(#[ExampleAttribute] $bar) { }
}

8. New static return type

In the existing versions of PHP, static is not a valid return type like self. So it would be a useful feature to the developers

class Foo
{
    public function test(): static
    {
        return new static();
    }
}

9. Inheritance with private methods

Previously, php used to apply the equal inheritance exams on public, protected and private methods. in different phrases: private strategies have to comply with the equal approach signature guidelines as included and public methods. this does not make feel, due to the fact that personal techniques may not be accessible by way of toddler lessons.

in the PHP 8 , inheritance checks are not performed on private methods anymore.

10. New mixed type

Some might call it a necessary evil: the mixed type many to have mixed feelings. a missing type can mean lots of things in PHP:

  • a function returns nothing or null
  • We’re expecting one of several types
  • We’re expecting a type that can’t be type hinted in PHP

11. Throw expression

This RFC changes throw from being a statement to being an expression, which makes it possible to throw exception in many new places:

$triggerError = fn () => throw new MyError();

$foo = $bar['offset'] ?? throw new OffsetDoesNotExist('offset');

Techspook

Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment