I'm using Object Oriented Programming to store User values to be used throughout a website. However i'm getting stuck on one particular issue. I'm trying to create a feature within a class that gets the User's values (after they login) to be stored to the Class instantiated. So far all the methods in the Class executes and the session becomes 'Logged-in' which is great. But the values I've tried to obtain from the User is not storing to the attributes within the 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);
}
Also the class does not seem to become instantiated as a trying to call a non-static function from the Class does not work. So the values aren't being obtained and the class isn't being instantiated. Please could anyone help me with this issue I'd appreciate it very much.