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
// }
// ]
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.