Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- if (isset($_POST['submitButton'])) {
- $db = new \mysqli("localhost", "user", "pass", "db");
- $user = new User("Juddling Stack", 123456);
- $user->setMoney(500.01);
- $player = new PlayerRegister($db, $user);
- if (! isset($_POST['money']) || ! is_numeric($_POST['money']))
- $player->addError("Enter Valid Wager Money");
- if ($_POST['money'] <= $user['money']) {
- $player->addError("You don't have that much money!");
- }
- try {
- $player->doSQL();
- } catch ( BaseException $e ) {
- foreach ( $e->getError() as $error ) {
- echo $error, " <br />";
- }
- }
- }
- /*******************
- *
- *
- *
- * THE classes
- */
- class Base {
- private $errors;
- function addError($error) {
- $this->error[] = $error;
- }
- function getErrors() {
- return $this->error;
- }
- function hasError() {
- return count($this->error) > 0 ? true : false;
- }
- }
- class BaseException extends Exception {
- private $error;
- function __construct(array $error) {
- $this->error = $error;
- }
- function getError() {
- return $this->error;
- }
- }
- class PlayerRegister extends Base {
- private $user;
- private $db;
- function __construct($db, User $user) {
- $this->db = $db;
- $this->user = $user;
- }
- function doSQL() {
- if ($this->hasError()) {
- throw new BaseException($this->getErrors());
- }
- $result = $this->db->query("SELECT * FROM someTable WHERE id={$user['id']}");
- $row = $result->fetch_assoc();
- if ($row) {
- /*
- * run some queries, give user some item
- */
- }
- }
- }
- class User extends Base {
- private $name;
- private $money;
- private $id;
- function __construct($name, $id) {
- $ths->name = $name;
- $this->id = $id;
- }
- function setMoney($money) {
- $this->money = $money;
- }
- function getName() {
- return $this->name;
- }
- function getMoney() {
- return $this->money;
- }
- function getID() {
- return $this->id;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement