Why PHP 8 will be awesome

PHP 8 will be available very soon. It’s a major version with breaking changes, but also with excellent features.

Permalink to heading Disclaimer Disclaimer

It’s a personal selection, not an exhausting list.

Permalink to heading Small details but great features Small details but great features

Again, it’s not an exhaustive list, but some changes will be handy for our daily PHP routines.

Permalink to heading ::class available for objects ::class available for objects

Instead of calling an intermediary get_class() on objects, you will be able to do that directly:

$myObject = new MyClass();
var_dump($myObject::class);

Source

Permalink to heading Trailing commas for parameters Trailing commas for parameters

In PHP 7, you can add trailing commas in arrays like that:

$myArray = [ "pull", "push", "jump",];

But you cannot do this with function and method parameters. In PHP8, you can!

new Uri(
    $scheme,
    $user,
    $pass,
    $host,
    $port,
    $path,
    $query,
    $fragment, // <-- Huh, this is allowed!
);

Source

Permalink to heading str_* new magic str_* new magic

I read about str_contains, str_starts_with() or str_ends_with() in rfc. Simpler, better, stronger than strpos or any regular expression.

Permalink to heading Break things for the win Break things for the win

PHP 8 will throw many more errors by default than PHP 7. Everything deprecated before PHP 8 has been removed!

Permalink to heading The @ operator The @ operator

This dark lord of PHP operators will no longer silence fatal errors!!!

Prepare for battle

Permalink to heading Fatal error for static calls of non-static methods Fatal error for static calls of non-static methods

The following code already triggers some errors, but in PHP 8, it will raise a fatal error!

class MyClass {
    public function myMethod() {}
}
MyClass::myMethod();

Permalink to heading New default error reporting level New default error reporting level

E_ALL will be the default value. Again, no more silenced errors.

Permalink to heading Don’t mess with undefined! Don’t mess with undefined!

PHP 8 will convert many warnings and notices into errors, for example:

  • undefined variables
  • division by zero

Permalink to heading The JIT disclaimer The JIT disclaimer

It’s not possible to write any post about PHP 8 without celebrating the JIT birth \0/.

The Just In Time compilation should improve performances, somehow. Before PHP 8, PHP executes code by converting PHP code into instructions (~ opcodes) that the virtual machine runs.

The JIT compiler converts PHP code into x86 machine code, which runs directly on the CPU.

However, as Nikita Popov said, it will be significant for code that involves mathematical calculations, not the average code.

Do not expect the same wow effect as with migrations from PHP 5 to PHP 7.

Permalink to heading Killer features Killer features

Some features will be decisive.

Permalink to heading Union types Union types

Before PHP 8, union types were possible only in phpdoc:

/**
* @var int|float $number
*/
private $number;

With PHP 8, you will write:

private int|float $number

The idea is to move more type information from phpdoc into function signatures. Void won’t be available in union types, though, but it would not make sense anyway.

Source

Permalink to heading static return type static return type

It will follow the same purpose as the static keyword. Your method should return something from the current (~ child) class, not the parent class.

class MyClass {
    public function myMethod(): static {
        return new static();
    }
}

Awesome!

Permalink to heading Throw as expression Throw as expression

Moving Throw from statement to expression will allow the following usage in PHP 8:

$condition || throw new Exception();

Source

Love the idea of using Throw in arrow functions or ternaries.

Permalink to heading Conclusion Conclusion

PHP 8 is better, faster, stronger. Can’t wait to use it. It looks like the first step toward making PHP a type-safe programming language.

I do my best to update all my contents, but keep it mind that "Why PHP 8 will be awesome" has been published many months ago.