1 回答

TA貢獻1779條經(jīng)驗 獲得超6個贊
您可以聲明getStatuses為Cake,static或使用公共常量。例如:
class Cake
{
// with static variables
private static $statuses = [
'not_ready' => 'Not Ready',
'almost_ready' => 'Almost Ready',
'ready' => 'Ready',
'too_late' => 'Too late',
];
public static function getStatuses()
{
return self::$statuses;
}
// or with public const
public const STATUSES = [
'not_ready' => 'Not Ready',
'almost_ready' => 'Almost Ready',
'ready' => 'Ready',
'too_late' => 'Too late',
];
}
這似乎是合理的,因為返回值不是實例而是特定于類的。
然后你可以使用:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('status', ChoiceType::class, [
'choices'=> Cake::getStatuses(),
]);
// or
$builder->add('status', ChoiceType::class, [
'choices'=> Cake::STATUSES,
]);
}
如果選擇實際上取決于給定的 Cake 實例,您可以通過選項數(shù)組或使用表單事件傳遞它。
- 1 回答
- 0 關注
- 81 瀏覽
添加回答
舉報