使用面向对象的编程来存储用户值

时间:2018-04-19 19:43:26

标签: php arrays class oop methods

我使用面向对象编程来存储要在整个网站中使用的用户值。但是,我遇到了一个特定的问题。我试图在一个类中创建一个功能,该功能可以将用户的值(登录后)存储到实例化的类中。到目前为止,Class中的所有方法都会执行,并且会话变为“登录”。这很棒。但是我试图从用户那里获得的值并没有存储到类中的属性中。

class Users {

public $User_ID;     
public $Password;
public $First_name;
public $Surname;
public $Email;
public $User_lvl;

public static function authenticate($Email, $Password) {
global $database;
$sql  = "SELECT * FROM Users ";
$sql .= "WHERE Email = '{$Email}' ";
$sql .= "AND Password = '{$Password}' ";
$sql .= "LIMIT 1";
self::find_by_sql($sql);
$result_array = self::find_by_sql($sql);
    return !empty($result_array) ? array_shift($result_array) : false;
  }


public static function find_by_sql($sql="") {
global $database;
$result_set = $database->query($sql);
$object_array = array();
while ($row = $database->fetch_array($result_set)) {
  $object_array[] = self::instantiate($row);
    }
 return $object_array;
}

public function fetch_array($result_set) {
return mysqli_fetch_array($result_set);
 }

private static function instantiate($row) {
$object = new self;
    foreach($row as $attribute=>$value){
      if($object->has_attribute($attribute)) {
        $object->$attribute = $value;
      }
    }
return $object; 
}

private function has_attribute($attribute) {
  $object_vars = get_object_vars($this);
  return array_key_exists($attribute, $object_vars);
}

此类似乎没有被实例化为尝试从类中调用非静态函数不起作用。 因此,没有获得这些值,并且没有实例化该类。请任何人帮我解决这个问题,我非常感激。

2 个答案:

答案 0 :(得分:1)

我为您准备了可以使用的OOP课程。

  1. 将全局移至DBService基类。

  2. self调用更改为用户实例。

    class User {
    
        private $userId;
        private $password;
        private $firstName;
        private $surname;
        private $email;
        private $userLevel;
    
        function __construct($userId, $password, $firstName, $surname, $email, $userLevel) {
            $this->userId = $userId;
            $this->password = $password;
            $this->firstName = $firstName;
            $this->surname = $surname;
            $this->email = $email;
            $this->userLevel = $userLevel;
        }
    
        function getUserId() {
            return $this->userId;
        }
    
        function getPassword() {
            return $this->password;
        }
    
        function getFirstName() {
            return $this->firstName;
        }
    
        function getSurname() {
            return $this->surname;
        }
    
        function getEmail() {
            return $this->email;
        }
    
        function getUserLevel() {
            return $this->userLevel;
        }
    
        function setUserId($userId) {
            $this->userId = $userId;
        }
    
        function setPassword($password) {
            $this->password = $password;
        }
    
        function setFirstName($firstName) {
            $this->firstName = $firstName;
        }
    
        function setSurname($surname) {
            $this->surname = $surname;
        }
    
        function setEmail($email) {
            $this->email = $email;
        }
    
        function setUserLevel($userLevel) {
            $this->userLevel = $userLevel;
        }
    
    }
    
    class UserDBServiceImpl extends DBService implements UserDBService {
    
        public function authenticate($Email, $Password) {
            global $database;
            $sql = "SELECT * FROM Users ";
            $sql .= "WHERE Email = '{$Email}' ";
            $sql .= "AND Password = '{$Password}' ";
            $sql .= "LIMIT 1";
            self::find_by_sql($sql);
            $result_array = self::find_by_sql($sql);
            return !empty($result_array) ? array_shift($result_array) : false;
        }
    
        public function find_by_sql($sql = "") {
    
            $result_set = $database->query($sql);
            $object_array = array();
            while ($row = $database->fetch_array($result_set)) {
                $object_array[] = self::instantiate($row);
            }
            return $object_array;
        }
    
        public function fetch_array($result_set) {
            return mysqli_fetch_array($result_set);
        }
    
        private function instantiate($row) {
            $object = new self;
            foreach ($row as $attribute => $value) {
                if ($object->has_attribute($attribute)) {
                    $object->$attribute = $value;
                }
            }
            return $object;
        }
    
        private function has_attribute($attribute) {
            $object_vars = get_object_vars($this);
            return array_key_exists($attribute, $object_vars);
        }
    
    }
    
    class DBService {
    
        private $database = null;
    
        private function init() {
            $this->database = ""; // Create connection to database
        }
    
        protected static function getInstance() {
            if ($database == null)
                init();
            return $database;
        }
    
    }
    
    interface UserDBService {
    
        public function authenticate($Email, $Password);
    
        public function find_by_sql($sql = "");
    
        public function fetch_array($result_set);
    
        function instantiate($row);
    
        function has_attribute($attribute);
    }
    

答案 1 :(得分:-1)

使用标准property_exists()代替has_attribute()。另外,使用mysqli_fetch_assoc的{​​{1}}内容来阻止获取混合的数字/关联键。