对于某个项目,我有一个class Member
和一个class Certificate
。
会员拥有证书(或不具有证书),证书必须绑定到会员。
有没有办法通过$member->certificate
访问证书,有成员通过$certificate->member
而无需进行无限循环?
答案 0 :(得分:0)
好的,由于成员可以在没有证书的情况下存在,因此您的构造函数将具有null的默认证书。由于证书必须有成员,因此您将指示此成员在施工期间通过。
class Member
{
public $certificate;
public function __construct(Certificate $certificate = null)
{
$this->certificate = $certificate;
}
}
class Certificate
{
public $member;
public function __construct(Member $member)
{
$this->member = $member;
$member->certificate = $this;
}
}
$member1 = new Member();
$certificate1 = new Certificate($member1);
顺便说一下,这个无限循环的东西一定是个笑话......