Jest to przykładowy game framework, dla treningu. Mam w nim 4 klasy User, UserManager, Character, CharacterManager, oto one :
<?php class User { function __construct() { } private $_login = null; private $_password = null; private $_email = null; private $_id = null; private $_logged = null; // tutaj dalej sa standardowe settery i gettery } ?>
<?php class UserManager { function __construct($user) { $this->_user = $user; try{ $this->_pdo = new PDO('mysql:host=localhost;dbname=rsfc;', '', ''); $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $error){ } } private $_user; public function Logon(){ $db = $this->_pdo->prepare('SELECT * FROM user_account WHERE login = :login AND password = :password'); $db->execute(array(':login' => $this->_user->getLogin(), ':password' => $this->_user->getPassword())); try{ if($user = $db->fetch()){ $this->_user->setId($user[id]); $this->_user->setLogged(1); $this->_user->setSession(); } else{ throw new Exception("Nie ma Cie w bazie ! Nie zostales zalogowany :("); } }catch(Exception $error){ } } } ?>
<?php class Character { function __construct() { } private $_strenght; private $_speed; private $_dynamic; private $_condition; private $_pain; private $_endurance; private $_iq; private $_coordination; private $_technic; private $_flex; private $_life; private $_level; private $_experince; private $_needexperince; private $_money; private $_reputation; private $_movepoints; private $_id = null; //settery // tutaj tez standardowe settey i gettery } ?>
<?php class CharacterManager { function __construct($character) { $this->_character = $character; try{ $this->_pdo = new PDO('mysql:host=localhost;dbname=rsfc;', '', ''); $this->_pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); }catch(PDOException $error){ } } private $_character; private $_pdo; public function render(){ include './views/character.php'; } public function upgradeStatistic($statistic, $how_much){ $db = $this->_pdo->prepare('UPDATE user_character SET '.$statistic.' = '.$statistic.' + :how_much WHERE id = :id'); } public function downgradeStatistic($statistic, $how_much){ $db = $this->_pdo->prepare('UPDATE user_character SET '.$statistic.' = '.$statistic.' - :how_much WHERE id = :id'); } public function setStatistics(){ try{ $db = $this->_pdo->prepare('SELECT * FROM user_character WHERE id = :id'); if($character = $db->fetch()){ $this->_character->setCondition($character['condition']); $this->_character->setCoordination($character['coordination']); $this->_character->setDynamic($character['dynamic']); $this->_character->setEndurance($character['endurance']); $this->_character->setExperince($character['experince']); $this->_character->setFlex($character['flex']); $this->_character->setIq($character['iq']); $this->_character->setLevel($character['level']); $this->_character->setLife($character['life']); $this->_character->setMoney($character['money']); $this->_character->setMovePoints($character['move_points']); $this->_character->setNeedExperince($character['needexperince']); $this->_character->setPain($character['pain']); $this->_character->setReputation($character['reputation']); $this->_character->setSpeed($character['speed']); $this->_character->setStrenght($character['strenght']); $this->_character->setTechnic($character['technic']); }else{ throw new Exception('Nie moge uaktualnic statystyk :('); } } catch(Exception $error){ } } } ?>
Index.php
<?php require 'class/Character.php'; require 'class/CharacterManager.php'; require 'class/User.php'; require 'class/UserManager.php'; $User = new User; $User->setLogin(''); $UserManager = new UserManager($User); $UserManager->Logon(); try{ if($User->getLogged() == 1){ $Character = new Character; $Character->setIdFromSession(); $CharacterManager = new CharacterManager($Character); $CharacterManager->upgradeStatistic('strenght', 5); $CharacterManager->setStatistics(); }else{ throw new Exception('Nie jestes zalogowany !'); } }catch(Exception $error){ } ?>
Pytania:
1. Głowne - czy ten kod jest zgodny z OOP ?
2. Czy metoda Logon() powinna być w klasie UserManager czy w osobnej np. Login
3. Czy tworzenie obiektu User a potem przekazywanie go do UserManager w celu obróbki itp jest prawidłowe ? Czy w klasie UserManager powinna być metoda tworząca obiekt User ? (to tyczy się również klas Character i CharacterManager)
Proszę o wytknięcie mi błedów i sposobów w jaki mogę je poprawić
