PHP 8 will be available very soon. It's a major version with breaking changes, but also with excellent features.
Disclaimer
It's a personal selection, not an exhausting list.
Small details but great features
Again, it's not an exhaustive list, but some changes will be handy for our daily PHP routines.
::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);
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!
);
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.
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!
The @
operator
This dark lord of PHP operators will no longer silence fatal errors!!!
Prepare for battle
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();
New default error reporting level
E_ALL
will be the default value. Again, no more silenced errors.
Don't mess with undefined!
PHP 8 will convert many warnings and notices into errors, for example:
- undefined variables
- division by zero
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.
Killer features
Some features will be decisive.
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.
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!
Throw
as expression
Moving Throw
from statement to expression will allow the following usage in PHP 8:
$condition || throw new Exception();
Love the idea of using Throw
in arrow functions or ternaries.
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.