PHP Déclaration de propriétés de type callable

Par FACHINA, il y a 11 mois


<?php /** * @author Anderson FACHINA <anderson.fachina@outlook.fr> * * @property string $columnProperty * Column property to be used in the export (Must match a property of the entity) if not a string use the normalizer to format the value * @property string $columnTitle * Column title to be shown in the exportation result * @property string|null $columnDescription * Column description * @property bool $excludeFromExcel * Whether to exclude the column from the Excel export * @property bool $excludeFromPDF * Whether to exclude the column from the PDF export * @property bool $isStringifier * Whether to use the stringifier to export the column (Only one column must be a stringifier) * @property callable|null $normalizer * Normalizer to be used to format the column value */ class ExportSchemaProps { public function __construct( public string $columnProperty, public string $columnTitle, public ?string $columnDescription = null, public bool $excludeFromExcel = false, public bool $excludeFromPDF = false, public bool $isStringifier = false, public callable|null $normalizer = null ){ } }

J'ai créer cette classe pour un module d'exportation mais PHP Intelephense renvoie cette erreur:

Property ExportSchemaProps::$normalizer cannot have type ?callable

2 réponses

Robert Linkous, il y a 11 mois

A common workaround is to use Closure|null instead of callable|null if the callable is always expected to be a closure. It also provides better auto-completion support in some IDEs.

StreetPlayer, il y a 11 mois

Hey !
Juste pour te prévenir : callable|null n’est pas une syntaxe valide en PHP. Il faut écrire ?callable $normalizer = null.