freeman701

User Class

Dec 4th, 2021 (edited)
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.56 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Vendor\App;
  4.  
  5. class User
  6. {
  7.     private $bug;
  8.     private $email;
  9.     private $password;
  10.     private $fillable = ['email'];
  11.     private $accessible = ['email'];
  12.     private $isAdmin = false;
  13.  
  14.     public function __construct(array $params = [])
  15.     {
  16.         if (count($params)) {
  17.             foreach ($params as $key => $value) {
  18.                 $this->$key = $value;
  19.             }
  20.         }
  21.     }
  22.  
  23.     public function __set($name, $value)
  24.     {
  25.         if (!in_array($name, $this->fillable)) return false;
  26.  
  27.         if (isset($this->$name))
  28.         {
  29.             $this->$name = $value;
  30.         }
  31.     }
  32.  
  33.     public function __get($name)
  34.     {
  35.         if (!in_array($name, $this->accessible)) return null;
  36.  
  37.         return $this->$name ?? null;
  38.     }
  39.  
  40.     public function __toString()
  41.     {
  42.         $data = [];
  43.  
  44.         foreach ($this->accessible as $key) {
  45.             $data[$key] = $this->$key;
  46.         }
  47.  
  48.         return json_encode($data);
  49.     }
  50.  
  51.     public function login()
  52.     {
  53.         return 'logging in as ' . $this->email . ' ...';
  54.     }
  55.  
  56.     public function logout()
  57.     {
  58.         return "$this->email has logged out...";
  59.     }
  60.  
  61.     public function setPassword($string)
  62.     {
  63.         $this->password = $string;
  64.         return $this;
  65.     }
  66.  
  67.     public function getPassword()
  68.     {
  69.         return $this->password;
  70.     }
  71.  
  72.     public function setEmail($string)
  73.     {
  74.         $this->email = $string;
  75.         return $this;
  76.     }
  77.  
  78.     public function getEmail()
  79.     {
  80.         return $this->email;
  81.     }
  82. }
Add Comment
Please, Sign In to add comment