Advertisement
Guest User

Procedural PHP to where?

a guest
Oct 2nd, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.92 KB | None | 0 0
  1. <?php
  2.  
  3.  
  4. if (isset($_POST['submitButton'])) {
  5.    
  6.     $db = new \mysqli("localhost", "user", "pass", "db");
  7.    
  8.     $user = new User("Juddling Stack", 123456);
  9.     $user->setMoney(500.01);
  10.    
  11.     $player = new PlayerRegister($db, $user);
  12.    
  13.     if (! isset($_POST['money']) || ! is_numeric($_POST['money']))
  14.         $player->addError("Enter Valid Wager Money");
  15.    
  16.     if ($_POST['money'] <= $user['money']) {
  17.         $player->addError("You don't have that much money!");
  18.     }
  19.    
  20.     try {
  21.         $player->doSQL();
  22.     } catch ( BaseException $e ) {
  23.        
  24.         foreach ( $e->getError() as $error ) {
  25.             echo $error, " <br />";
  26.         }
  27.     }
  28. }
  29.  
  30.  
  31. /*******************
  32.  *
  33.  *
  34.  *
  35.  *  THE classes
  36.  */
  37.  
  38.  
  39. class Base {
  40.     private $errors;
  41.  
  42.     function addError($error) {
  43.         $this->error[] = $error;
  44.     }
  45.  
  46.     function getErrors() {
  47.         return $this->error;
  48.     }
  49.  
  50.     function hasError() {
  51.         return count($this->error) > 0 ? true : false;
  52.     }
  53. }
  54.  
  55.  
  56. class BaseException extends Exception {
  57.     private $error;
  58.  
  59.     function __construct(array $error) {
  60.         $this->error = $error;
  61.     }
  62.  
  63.     function getError() {
  64.         return $this->error;
  65.     }
  66. }
  67.  
  68.  
  69. class PlayerRegister extends Base {
  70.     private $user;
  71.     private $db;
  72.  
  73.     function __construct($db, User $user) {
  74.         $this->db = $db;
  75.         $this->user = $user;
  76.     }
  77.  
  78.     function doSQL() {
  79.         if ($this->hasError()) {
  80.             throw new BaseException($this->getErrors());
  81.         }
  82.         $result = $this->db->query("SELECT * FROM someTable WHERE id={$user['id']}");
  83.         $row = $result->fetch_assoc();
  84.  
  85.         if ($row) {
  86.             /*
  87.              * run some queries, give user some item
  88.             */
  89.         }
  90.     }
  91. }
  92.  
  93.  
  94. class User extends Base {
  95.     private $name;
  96.     private $money;
  97.     private $id;
  98.  
  99.     function __construct($name, $id) {
  100.         $ths->name = $name;
  101.         $this->id = $id;
  102.     }
  103.  
  104.     function setMoney($money) {
  105.         $this->money = $money;
  106.     }
  107.  
  108.     function getName() {
  109.         return $this->name;
  110.     }
  111.  
  112.     function getMoney() {
  113.         return $this->money;
  114.     }
  115.  
  116.     function getID() {
  117.         return $this->id;
  118.     }
  119. }
  120.  
  121.  
  122. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement