Czy jest możliwe zrobić coś takiego, że na podstawie nazw kolumn tabeli utworzymy klase z taką zawartością, przykładowo chcę zrobić tak:

Pola private $zmienna to odpowiedniki nazw kolumn w tabeli, jak zrobić automat do deklaracji?
  1. class User
  2. {
  3. public $id = "";
  4. public $login = "";
  5. public $passwd = "";
  6. public $firstname = "";
  7. public $lastname = "";
  8. public $level = "";
  9. public $user_id = 0;
  10. public $email = "";
  11. public $mobile = "";
  12. public $data_add = "";
  13. public $username = "";
  14.  
  15. public function __construct(){}
  16. public function addUser(){}
  17. public function deleteUser(){}
  18. public function updateUser(){}
  19. }


Potem wypełniam taką klasę danymi już automatycznie w funkcji login():

  1. class Authentication
  2. {
  3. private $db;
  4. public $user;
  5.  
  6. public function __construct(){
  7. $this->db = DBconnect::getInstance();
  8. $this->user = new User;
  9. }
  10.  
  11. public function get_param()
  12. {
  13. if( isset($_POST["login"]) and isset($_POST["passwd"]) ) {
  14. $this->user->login = $_POST["login"];
  15. $this->user->passwd = $_POST["passwd"];
  16. $_SESSION["login"] = $this->user->login;
  17. $_SESSION["passwd"] = $this->user->passwd;
  18. return true;
  19. }elseif(isset($_SESSION["login"]) and isset($_SESSION["passwd"]) ){
  20. $this->user->login = $_SESSION["login"];
  21. $this->user->passwd = $_SESSION["passwd"];
  22. return true;
  23. }else{
  24. return false;
  25. }
  26. }
  27.  
  28. public function login()
  29. {
  30. if( $this->get_param() )
  31. {
  32. $sql = "select * FROM users WHERE active = 1 and login = :login and passwd = md5(:passwd)";
  33. $query = $this->db->pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
  34. $query->bindParam(':login', $this->user->login, PDO::PARAM_STR);
  35. $query->bindParam(':passwd', $this->user->passwd, PDO::PARAM_STR);
  36. $query->execute();
  37.  
  38. if( $query->rowCount() > 0 ) {
  39.  
  40. $column_data = $query->fetchAll(PDO::FETCH_ASSOC);
  41. foreach($column_data as $outer_key => $array){
  42. foreach($array as $inner_key => $value){
  43. $this->user->$inner_key = $value;
  44. } }
  45.  
  46. }else{
  47.  
  48. $column_data = $query->fetchAll(PDO::FETCH_ASSOC);
  49. foreach($column_data as $outer_key => $array){
  50. foreach($array as $inner_key => $value){
  51. $this->user->$inner_key = "";
  52. } }
  53. }
  54. }
  55. }