New in Symfony 5.1: Added support for typed properties in PropertyInfo
March 9, 2020 • Published by Javier Eguiluz
Warning: This post is about an unsupported Symfony version. Some of this information may be out of date. Read the most recent Symfony Docs.
Contributed by
Kévin Dunglas
in #34557.
Symfony 5.1 will be released in May 2020. This is the first article of the series that shows the most important new features introduced by this Symfony version.
PHP 7.4, which was released on November 28, 2019, added support for typed properties which allow to define the type of variables when declaring them:
1 2 3 4 5 6 7 8 9 10 11
use Symfony\Component\HttpFoundation\Request;
class SomeClass
{
public int $id;
protected string $name;
private ?bool $logResult;
public Request $request;
// ...
}
The PropertyInfo component extracts information about the properties of PHP classes using several sources (Doctrine metadata, PHP reflection, PHPdoc config, etc.) In Symfony 5.1, we improved this component to also extract information from PHP typed properties.
Considering the previous example, this will be the result when using Symfony 5.1.:
1 2 3 4 5 6 7 8 9 10 11 12
$info = $propertyInfo->getTypes(SomeClass::class, 'logResult');
// $info = [
// class Symfony\Component\PropertyInfo\Type (6) {
// private $builtinType => string(4) "bool"
// private $nullable => bool(true)
// private $class => NULL
// private $collection => bool(false)
// private $collectionKeyType => NULL
// private $collectionValueType => NULL
// }
// ]
Help the Symfony project!
As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.
Comments are closed.
To ensure that comments stay relevant, they are closed for old posts.