PHP 8.0 brings new features, syntax additions, and bugfixes. For details on what's new in this version, see the release announcement. For details on migrating your code, refer to the Migrating from PHP 7.4.x to PHP 8.0.x guide.
Named arguments allow passing arguments to a function based on the parameter name rather than position. This way, function calls become self-documenting and arguments order becomes arbitrary.
In a function call, you can use the provided quick-fix Alt+Enter to add parameter names either one by one or to all arguments at once. If there are arguments in the list whose passed value matches the default value, PhpStorm highlights them as unused and provides a quick-fix to delete them.
With attributes, you can provide structured, syntactic metadata to declarations of classes, properties, functions, and so on. Unlike PHPDoc comments, attributes are supported and validated on the language level.
To create an attribute, add the
#[Attribute] marker on top of a class declaration. Inside the attribute, optionally provide the target and repeatability flags. To use code completion, press Ctrl+Space.
Several code inspections help to ensure that attributes are used correctly:
The Class cannot be used as attribute inspection verifies that a class is annotated with
#[Attribute]in its arguments declaration.
The Non-applicable attribute target declaration inspection verifies that the attribute is annotated with
#[Attribute::Target]in its arguments declaration, and is therefore applied to the correct target such as a class or a method,
The Non-repeatable attribute inspection highlights attributes that are repeated without the
Attribute::IS_REPEATABLEflag set in its arguments declaration.
A Union Type accepts values of multiple different types rather than a single one. Union types are specified as
Type1|Type2|... and can be used in all places where regular types are accepted. All existing PhpStorm coding assistance features fully support union types.
If a union type is provided via PHPDoc, PhpStorm offers a quick-fix Alt+Enter for converting it to a native union type.
With natively declared union types, PhpStorm can analyze calls and detect problem areas. The checks work on all levels: properties, arguments, and return values.
Constructor property promotion
With Constructor property promotion, you can declare and initialize class properties directly in the constructor. If you provide a visibility modifier such as
private to a constructor argument, the argument will be interpreted as an object property and its value will be assigned to this property. This makes objects smaller and more readable and helps reducing the amount of boilerplate code in cases where you initialize variables through a constructor.
You can convert constructor arguments to promoted properties with a quick-fix Alt+Enter.
PhpStorm verifies that promoted properties are used in accordance with the PHP 8 constraints. For example, a promoted property cannot be declared outside a constructor or inside an abstract constructor, and variadic parameters cannot be promoted.
A match expression is similar to
switch but provides safer semantics and can return a value. PhpStorm detects
switch statements that can be converted to
match expressions and provides a dedicated quick-fix Alt+Enter to do this.
Several code inspections help to ensure that
match expressions are used correctly:
If the expression argument's type does not match any of the specified conditions, the expression will cause a
Fatal Error. In such cases, you can add a
defaultarm by using the provided quick-fix.
The Unused 'match' condition inspection highlights the conditions that are never matched and suggests deleting them.
The Unused condition inspection detects duplicate conditions that will never run. You can navigate between them and remove either the entire arm or only the duplicate value from a list of values.
The Duplicate arm in 'match' expression inspection detects identical bodies in different arms, which can be merged into one to reduce the size of the block.
The 'match' expression can be replaced with ternary expression inspection will detect expressions with a single arm and a
defaultarm and let you replace them with ternary expressions.
If a match expression only has a default arm, then it is probably redundant. The Redundant 'match' expression inspection will highlight such expressions and let you remove them.
With the nullsafe operator
?->, you can replace nested null check conditions with chained calls.
PhpStorm verifies that the nullsafe operator is not erroneously used in write context or as a reference.
Starting with PHP 8, you can catch exceptions without capturing them to variables. The Unused local variable inspection detects the variables that are not used inside the
catch statement and provides a quick-fix Alt+Enter to remove them:
In PHP 8, throwing exceptions is allowed in all places where expressions are accepted, for example, in arrow functions, the coalesce operator
??, and the ternary operator
?:. PhpStorm provides a live template that lets you quickly add a
throw expression. To apply it, type
thr and press Tab.
Using ::class on objects
In previous PHP versions, to get a class FQN, you could use
ClassName::class. On objects, however, you had to call
get_class(). In PHP 8,
get_class() calls can be safely replaced with
$object::class. PhpStorm provides a quick-fix Alt+Enter for this and will also warn if
::class is used inappropriately.
New functions for strings
PHP 8 introduces several new functions for working with strings. The str_contains function checks whether a string is contained in another string. The str_starts_with and str_ends_with functions are used for determining if a string starts or ends with a specific substring. The 'str*' calls can be replaced with PHP 8 'str_*' calls inspection highlights the
substr calls that can be replaced with their modern and more self-explanatory alternatives and provides the corresponding quick-fixes Alt+Enter.