The following is an excerpt from some code I wrote to assign the $user->privilege based on a method from that same class. It seems excessively repetitive, and I am wondering if there is something I can do to make it more readable — given that I haven’t seen this kind of repetition too much in codes I have looked at.
$user -> privileges = $user -> get_privileges ( $user -> username );
It doesn’t look particularly repetitious to me, but it is a little unusual to be assigning an object’s property based on a method outside the class. Instead, this might be better handled inside the object constructor, eliminating the need for you to remember to set the property when coding:
And as an alternative to
$this->privileges = $this->get_privileges();called in the constructor, you might just set$this->privilegesinside theget_privileges()method. Then you can just call it as$this->get_privileges()in the constructor, no assignment necessary. Either way works.