Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

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

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.

New in Symfony 5.1: Added support for typed properties in PropertyInfo symfony.com/blog/new-in-symfony-5-1-added-support-for-typed-properties-in-propertyinfo

Tweet this

Comments

Cool thing! How will it work with arrays ? Does the component, in case of array typed property, also check the phpdoc to get the details ? (For example: stdClass[])
Isn't "bool" of type string(4) instead of string(6) ?
@Thomas you are right! This is now fixed.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.