Symfony News

New in Symfony 5.1: Added support for typed properties in PropertyInfo

Kévin Dunglas

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(6) "bool"
//         private $nullable             => bool(true)
//         private $class                => NULL
//         private $collection           => bool(false)
//         private $collectionKeyType    => NULL
//         private $collectionValueType  => NULL
//     }
// ]

Be trained by Symfony experts - 2020-03-10 Clichy - 2020-03-10 Clichy - 2020-03-12 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook