Advertisement
Guest User

bindingmodel

a guest
Oct 30th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.38 KB | None | 0 0
  1. <?php
  2.  
  3. class UserRegForm
  4. {
  5.     private $username;
  6.     private $password;
  7.     private $confirm;
  8.     private $email;
  9.    
  10.     public function getUsername()
  11.     {
  12.         return $this->username;
  13.     }
  14.    
  15.     public function setUsername($username)
  16.     {
  17.         $this->username = $username;
  18.     }
  19.  
  20.     public function getPassword()
  21.     {
  22.         return $this->password;
  23.     }
  24.    
  25.     public function setPassword($password)
  26.     {
  27.         $this->password = $password;
  28.     }
  29.    
  30.     public function getConfirm()
  31.     {
  32.         return $this->confirm;
  33.     }
  34.    
  35.     public function setConfirm($confirm)
  36.     {
  37.         $this->confirm = $confirm;
  38.     }
  39.    
  40.     public function getEmail()
  41.     {
  42.         return $this->email;
  43.     }
  44.  
  45.     public function setEmail($email)
  46.     {
  47.         $this->email = $email;
  48.     }
  49.  
  50.  
  51. }
  52.  
  53. class UserController
  54. {
  55.     public function registerPost(UserRegForm $form)
  56.     {
  57.         if ($form->getPassword() != $form->getConfirm()) {
  58.             // do smth;
  59.         }
  60.     }
  61. }
  62.  
  63. $form = new UserRegForm();
  64. $formInfo = new \ReflectionClass($form);
  65. foreach ($_POST as $key => $value) {
  66.     if (!$formInfo->hasProperty($key)) {
  67.         throw new Exception("POST malformed");
  68.     }
  69.    
  70.     $setterMethod = 'set' . ucfirst($key);
  71.     $form->$setterMethod($value);
  72. }
  73. $controller = new UserController();
  74. $controller->registerPost($form);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement