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

I have this user class

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

  public function User() {
      //> Check if the user is logged in with a cookie-database and set $logged=true;
  }  

  public function isLogged() {}
  public function editPerms() {}

  //> other methods    
}

Well, now, given that I cannot have more than one user registered (of course, because we are talking about a single HTTP request) Where should I store the link to my istance?

This is the case when singleton would be useful, but nowadays everyone says that singleton is evil (e.g. static methods ).

I could do it $GLOBALS['currentUser'] = new User();and have it everywhere, but I think it's worse than singleton.

?
: . .

, -, ( ):

function app($class) {      //> Sample
    static $refs = array();

    if (!isset($refs[$class]))
        $refs[$class] = new $class();

    return $refs[$class];
}

//> usage app('User')->methods();

(IE, symfony )

+3
6

, . , "" "" .

, , , . , . , !

.

+6

. . , ( -, ) - :

, , . , , . : , ? , , , .:)

. , , , , , . , , " : , , ". , , , .:)

+3

. , User ( ?) . MVC, . , , ( , ).

. .

class AuthenticationService {
    /**
     * @var User
     */
    private $currentUser;

    public function __construct(Request $request) {
        // check if the request has an user identity
        // create a user object or do nothing otherwise
    }

    public function getCurrentUser() {
        return $this->currentUser;
    }
}

class User {
    public function editPerms(){}
}

// the client code
class Controller {
    private $auth;

    public function __construct(AuthenticationService $auth) {
        $this->auth = $auth;
    }

    public function handleRequest() {
        $currentUser = $this->auth->getCurrentUser();

        if ($currentUser === null) { // of course you could use Null Object Pattern
            // no user is logged in
        }

        // do something with the user object
    }
}

, : . , , - . AuthenticationService, . , , . DI . DI , .

"" - "," : ?" DI.

+2

, . , .

User. - , , :

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

   private static $_currentUser;
   public static function currentUser()
   {
     if (empty(self::$_currentUser))
     {
         @session_start();
         if (array_key_exists('current_user', $_SESSION))
         {
             self::$_currentUser = $_SESSION['current_user'];
         }
         else
         {
           // force login in or whatever else.
           // if you log in, make sure to call User::_setCurrentUser(); 
             return null; //or some special 'empty' user.
         }
     }
     return self::$_currentUser;
  }
  // you may consider making this public, but it is private because it is a bit
  // more secure that way.  
  private static function _setCurrentUser(User $user)
  {
     self::$_currentUser = $user;
     $_SESSION['current_user'] = $user;
  }

  public function User() {
   //> Check if the user is logged in with a cookie-database and set $logged=true;
  }  

  public function isLogged() {}
  public function editPerms() {}

  //> other methods    
}

// Usage
$pUser = User::currentUser();
+1

Misko Hevery . - . , . : , ? , : ( "" ). , :

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

, , , .

, , ( ?, ?), " " ​​...).

+1
  • , . "", .

, singleton factory, singleton factory (Auth) login(), User, HTTP- .

. , factory, , , , db .

class auth {
    private static $auth = null;
    private $user = null;

    // must use getAuth();
    private __construct(){};

    public getAuth() {
        if (is_null($this->auth) {
             $this->auth = new auth();
        }
        return $this->auth;       
    }

    public function login($user,$pass) {
        ... // check db for user,

        if ($dbrow->user_type == 'admin') {
            $this->user = new admin_user($dbrow);
        } else {
            $this->user = new normal_user($dbrow);
        }

        $this->user->setSession($db->getsession());
    }

    public function getUser() {
        return $this->user;
    }

    public function saveSession() {
        // store $this->user session in db
    }

    public function saveUser() {
        // store $this->user changes in db
    }
    ...
}

, - , , .

class normal_user extends user {
    ... getters and setters
    public function getName() {}
    public function setEmail() {}
    public function setprofile() {}
}

db, auth. () - auth- > login().

$me = new normal_user();
$me->setName();
echo $me->getName();

, db, $auth- > user;

you can then create a function in auth to use custom objects to create new users (during registration)

...
public function create(user $user) {
    // validate $user
    $this->user = $user;
    $this->saveUser();
}
...

you just need to make sure that you run the save functions at the end of execution ... maybe in the destructor ()

plain

0
source

All Articles