如何解决在调用父构造函数之前设置属性
在某些特定情况下,我们需要调用父构造函数parent::__construct();
after 属性初始化,如以下代码(Symfony 命令代码,更多详细信息参见Symfony documentation):
<?php
class CreateUserCommand extends Command
{
// ...
private EntityManagerInterface $entityManager;
public function __construct(EntityManagerInterface $entityManager,bool $requirePassword = false)
{
// best practices recommend to call the parent constructor first and
// then set your own properties. That wouldn't work in this case
// because configure() needs the properties set in this constructor
$this->entityManager = $entityManager;
$this->requirePassword = $requirePassword;
parent::__construct();
}
我想知道如果我们在 PHP 8 属性提升风格中创建构造函数,我们如何处理这种情况:
<?php
public function __construct(private EntityManagerInterface $entityManager,private bool
$requirePassword = false)
{
parent::__construct();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。