Symfony has been on a multi-year effort to add all PHP type declarations to its code. After adding parameter types in Symfony 5, and many return types in Symfony 6, we are getting close to end of the initiative in Symfony 7.0.

Adding type declarations need to be done with caution, as it breaks backwards compatibility in most cases. According to Symfony's release process such breaks are only allowed in major version releases (e.g. 6.0 and 7.0).

Spreading this effort across three major versions reduces the impact on libraries using Symfony components. It also gives the core team enough time to create the necessary tooling and review every change closely. A special thanks to Nicolas Grekas and Alexander M. Turek for leading this effort with their reviews.

Types in Symfony 7.0

The goal of Symfony 7.0 is to add every possible PHP type declaration. This means the following changes are in Symfony 7.0:

  • Add return types to all methods skipped or missed in Symfony 6.0;
  • Add the void return type were applicable;
  • Add types to all class properties, including public and protected ones.

Preparing your Applications

Per Symfony's release promise, each change has a corresponding deprecation notice in 6.4. This means you can prepare your applications in 6.4 before upgrading to 7.0. See the official documentation for more information about this process.

Symfony also comes with a type patcher utility to automatically add the missing return type declarations to your code:

1
2
3
4
5
# "-o" is important! This forces Composer to find all classes
$ composer dump-autoload -o

# patch all incompatible method declarations, see the docs for more information
$ ./vendor/bin/patch-type-declarations

Preparing your Open Source Libraries

When you're maintaining an open source library, preparing the code can be more involved. If you need to add a return type to your code, you might break backwards compatibility for your own users.

We acknowledge that releasing a new major version before December is not workable for all libraries. Based on the experiences from bundles maintained by the Symfony core team, we've postponed some impactful type declarations to Symfony 8.0 already (increasing the timeframe for a new major version with two years).

If you're a maintainer and an introduced type declaration breaks your library, please contact us in this meta issue as early as possible. We can't promise anything, but we might consider postponing more type declarations to Symfony 8.0. See the issue for the full instructions.

Thanks for your help towards type safety!

Published in #Symfony