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