Doctrine 2 Whats the Recommended Way to Access Properties?

后端 未结 6 1831
萌比男神i
萌比男神i 2020-12-14 20:04

I remember reading that in Doctrine 2 models, I should not set properties/fields public. How then would you expose these fields? The sandbox used get*() & <

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-14 20:22

    Rather than having seperate getter and setters, or even using the magic functions.. Is there problem with having something like this in the class

    public function Set($attrib, $value)
    {
        $this->$attrib = $value;    
    }
    
    public function Get($attrib)
    {
        return $this->$attrib;
    }   
    

    It makes it much easy to access the attributes and means they be dynamically set from key-pair arrays.. any comments? or alternative suggestions?

提交回复
热议问题