PhpStorm 2021.1 Help

PHP 8.0

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

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.

Adding named arguments

Attributes

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.

Creating a PHP attribute

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.

    Class not annotated with attribute
  • 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,

    Incorrect attribute target
  • The Non-repeatable attribute inspection highlights attributes that are repeated without the Attribute::IS_REPEATABLE flag set in its arguments declaration.

    Non-repeatable attribute

Union types

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.

Converting a PHPDoc union type to native declaration

With natively declared union types, PhpStorm can analyze calls and detect problem areas. The checks work on all levels: properties, arguments, and return values.

Type checks in union type declaration

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 public, protected, or 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.

Converting constructor arguments to promoted properties

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.

Promoted properties checks

Match expression

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.

Converting a switch statement to a match expression

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 default arm by using the provided quick-fix.

    Adding a default match arm
  • The Unused 'match' condition inspection highlights the conditions that are never matched and suggests deleting them.

    Deleting an unmatched condition
  • 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.

    Adding a default match
  • 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.

    Merging duplicate match arms
  • The 'match' expression can be replaced with ternary expression inspection will detect expressions with a single arm and a default arm and let you replace them with ternary expressions.

    Converting a match expression to a ternary expression
  • 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.

    Simplifying a match expression

Nullsafe operator

With the nullsafe operator ?->, you can replace nested null check conditions with chained calls.

Using the nullsafe operator

PhpStorm verifies that the nullsafe operator is not erroneously used in write context or as a reference.

Nullsafe operator inspections checks

Non-capturing catches

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:

Nullsafe operator inspections checks

Throw expression

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.

Throwing an exception in an expression

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.

Using ::class on an object

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 strpos and substr calls that can be replaced with their modern and more self-explanatory alternatives and provides the corresponding quick-fixes Alt+Enter.

Functions for working with strings
Last modified: 08 March 2021