Guest User

Untitled

a guest
Aug 11th, 2018
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. How can I can get a child class's attributes into the parent class's methods
  2. <?php
  3. require_once("user.php");
  4. $user = new User();
  5.  
  6. $user->auth("Scott", "rascal");
  7. echo $user->username;
  8. ?>
  9. <html>
  10. <head>
  11. <title>test</title>
  12. </head>
  13. <body>
  14.  
  15. </body>
  16. </html>
  17.  
  18. Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
  19.  
  20. Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
  21.  
  22. Notice: Undefined property: Database::$tableName in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 83
  23.  
  24. Notice: Undefined property: Database::$id in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 85
  25.  
  26. Notice: Undefined property: Database::$dbFields in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
  27.  
  28. Warning: Invalid argument supplied for foreach() in /Users/scottmcpherson/Sites/phpsites/projectx/application/models/db.php on line 24
  29.  
  30. <?php
  31. require_once("db.php");
  32.  
  33. class User extends Database{
  34.  
  35. public $dbFields = array('username', 'password');
  36.  
  37. public $tableName = "users";
  38. public $id;
  39. public $username;
  40. public $password;
  41.  
  42. public function auth($user, $pass){
  43. $this->username = $user;
  44. $this->password = $pass;
  45.  
  46. }
  47. }
  48. ?>
  49.  
  50. class Database{
  51.  
  52.  
  53. public $db;
  54.  
  55. public function __construct() {
  56. $this->connect();
  57. }
  58.  
  59. public function connect(){
  60. try {
  61. $this->db = new PDO("mysql:host=".DB_SERVER."; dbname=".DB_NAME, DB_USER, DB_PASS);
  62. $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
  63. } catch (PDOException $e) {
  64. echo 'Connection failed: ' . $e->getMessage();
  65. }
  66. }
  67.  
  68. public function properties() {
  69. $properties = array();
  70. foreach ($this->dbFields as $field) {
  71. if (isset($this->field) || property_exists($this, $field)) {
  72. $properties[$field] = $this->$field;
  73. }
  74. }
  75. return $properties;
  76. }
Add Comment
Please, Sign In to add comment