Which pattern should I use for my unique instance of the User class?

前端 未结 6 828
醉梦人生
醉梦人生 2021-01-14 18:51

I have this User class

class User{
  private    $logged = false;
  private    $id;

  public function User() {
      //> Check if the user is logged in wi         


        
6条回答
  •  无人及你
    2021-01-14 19:28

    The influence of Misko Hevery is pretty strong on me. So is his newable - injectable distinction. A user is not an injectable but a newable. What are the responsibilities of a user: should he be able to tell of himself whether he is logged in or not? There's a post of him where he talks about a similar problem: a credit card and charging it(self?). It happens to be a post about singletons, what you would like to make it:

    http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/

    That would leave it to an service to check whether the user is logged in or not, what rights he has on the site.

    It also means your architecture would change, your problem will become different (passing around the user?, where is it needed?, how will you have access to the 'checking user is logged in' service, ...).

提交回复
热议问题