我应该如何设计和使用User类?

时间:2014-01-10 13:04:45

标签: php object pdo

当我在程序化PHP编程中工作时,我会有一个登录页面,用户的会话将被创建并存储。然后,在每个页面上,我只显示了用户的名称并检索了我需要的信息。

但我很无奈,你会如何在对象编程中实现这一目标?

所以,程序性(login.php):

if ( $_POST['password'] != '' && $_POST['username'] != '' ) {
   // check if password&username combination exists in database etc.

   $_SESSION['user_id'] = $user_id;
   $_SESSION['username'] = $username;

}

我认为在OOP中会这样做:

// include user class

if ( $_POST['password'] != '' && $_POST['username'] != '' ) {
   // check if password&username combination exists in database etc.

$user = new User();
$user->id = $userId;
$user->username = $username;

但问题是:我在哪里创建会话?这个对象不会出现在这个“login.php”页面之外,因为我在那里创建它。如何让这些信息随处可访?

如何在不将查询放入用户类的情况下使用特殊类(比方说PDO)从数据库获取信息?

非常感谢,我只是无法理解结构

3 个答案:

答案 0 :(得分:1)

你会做一些事情:

class User{

private $id;
private $username;

   function __construct($userId, $username) {
       $this->$id = $userId;
       $this->$username = $username;
   }

}

if ( $_POST['password'] != '' && $_POST['username'] != '' ) {
   // check if password&username combination exists in database etc.
   //......
   //Store your session
    $_SESSION['user_id'] = $user_id;
    $_SESSION['username'] = $username;
    //Create user instance
    $user = new User($user_id, $username ); 
}

您还可以将数据访问层放在用户构造函数中,这样您只需要担心存储id会话。

user.php的:

class User{

private $id;
private $username, $first_name, $last_name;

   function __construct($userId) {
       $this->$id = $userId;
       //get user data from db using $id
       $this->$username = $row['username'];
       $this->$first_name = $row['first_name'];
       $this->$last_name = $row['last_name'];
   }

}

的login.php:

function get_userId($username, $password)
 // check if password&username combination exists in database etc.
 //then return Id
 //......   
return $userId
}

然后使用如下函数:

if ( $_POST['password'] != '' && $_POST['username'] != '' ) {
    $username = $_POST['username'];
    $password = $_POST['password'];
    $user_id = get_userId($username, $password);
    $_SESSION['user_id'] = $user_id;
}

然后你将使用如下的id创建用户实例:

session_start();
$user_id = $_SESSION['user_id']; 
$user = new User($user_id); 

答案 1 :(得分:0)

试试这个你差不多了

// include user class
session_start();

if ( $_POST['password'] != '' && $_POST['username'] != '' ) {
   // check if password&username combination exists in database etc.

$user = new User();
$user->id = $userId;
$user->username = $username;

$_SESSION['user_obj'] = serialize($user);

然后当您想要在另一个页面中再次访问它时:

// include user class
session_start();

$user = new User();
if ( isset( $_SESSION['user_obj'] ) {
    $user = unserialize($_SESSION['user_obj'];
}

答案 2 :(得分:0)

首先,您需要使用官方documentation

对于我的所有经验,我可以告诉你,只有你决定如何使用类和OOP。有许多模型和stadarts给你一些结构,但它是个人选择。

但在你的情况下,我可以建议您使用常规课程来检查和存储信息。

class User {
    private $id = null;
    private $username = null;
    private $isLoggedIn = false;
    public function __construct() {
        if (isset($_SESSION['userData']) && !empty($_SESSION['userData'])) { 
             // I want to think that you have more complex check for user session
             $this->id = $_SESSION['userData']['userId'];
             $this->username = $_SESSION['userData']['username'];
             $this->isLoggedIn = true;
        }
    }

    public function auth() {
        $_SESSION['userData'] = array()
        $result = false;
        // Here can be an password check
        if ($this->id && $this->username) { 
             $_SESSION['userData']['userId'] = $this->id;
             $_SESSION['userData']['username'] = $this->username ;
             $this->isLoggedIn = true;
             $result = true;
        }
        return $result;
    }
    public function isLoggedIn() {
        return $this->isLoggedIn;
    }

    public function userId($id = null) {
        if ($id != null) {
             $this->id = $id;
        }
        return $this->id;
    }

    public function userName($name = null) {
        if ($name != null) {
             $this->name = $name;
        }
        return $this->name;
    }

}

此课程的使用将简短而轻松

// include user class
session_start();
$user = new User();
if ($user->isLoggedIn()) {
    // user is Logged In
} elseif ( $_POST['password'] != '' && $_POST['username'] != '' ) { 
    // check if password&username combination exists in database etc.

    $user->userId($userId);
    $user->userName($username);
    $user->auth();
} else {
    echo 'Auth failed';
    die();
}
echo 'Hello, ' . $user->userName();