Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> klasa user - dobrze?
em_pl
post 3.05.2007, 19:39:59
Post #1





Grupa: Zarejestrowani
Postów: 65
Pomógł: 0
Dołączył: 10.12.2005
Skąd: Stalowa Wola / Wroclaw

Ostrzeżenie: (0%)
-----


Hej
Staram przestawić się na myślenie OOP. Stworzyłem klasę, która odpowiedzialna będzie za dane zalogowanego użytkownika.


  1. <?php
  2. class user extends DB{
  3. protected $db;
  4. private $fetch_mode = PDO::FETCH_ASSOC;
  5. private $prefix, $tableName, $permissions, $logged = 0, $id, $login, $name, $function;
  6. public function __construct($prefix) {
  7. $this->db = parent::getInstance();
  8. $this->prefix = $prefix;
  9. $this->tableName = $prefix.'users';
  10. if(!empty($_COOKIE['sid_'.$this->prefix])){
  11. $sid = &$_COOKIE['sid_'.$this->prefix];
  12. $sql = $this->db->prepare('SELECT * FROM '.$this->tableName.' WHERE sid=:sid');
  13. $sql->bindValue(':sid', $sid, PDO::PARAM_STR);
  14. $sql->execute();
  15. $row = $sql->fetch($this->fetch_mode);
  16. $sql->closeCursor();
  17. if($row['id']){
  18. $this->id = $row['id'];
  19. $this->login = $row['login'];
  20. $this->name = $row['name'];
  21. $this->function = $row['function'];
  22. $this->permissions = new userPermissions($this->prefix, $this->id);
  23. $this->logged  = 1;
  24. }
  25. }
  26. }
  27.  
  28. public function login($login, $password){...}
  29. public function logout(){...}
  30. public function getName(){
  31. return $this->name;
  32. }
  33. public function getFunction(){
  34. return $this->function;
  35. }
  36. public function getLogged(){
  37. return $this->logged;
  38. }
  39.  
  40. }
  41. ?>

Obiekt tej klasy będzie zawierał informacje o użytkowniku (jeśli jest on zalogowany). Dodatkowo zmienna $permission będzie obiektem klasy userPermission:

  1. <?php
  2. class userPermissions extends DB{
  3. protected $db;
  4. private $userId, $users, $portalContent, $categories, $tableName;
  5. private $fetch_mode = PDO::FETCH_ASSOC;
  6. public function __construct($prefix, $userId) {
  7. $this->db = parent::getInstance();
  8. $this->tableName = $prefix.'user_functions';
  9. $sql = $this->db->prepare('SELECT * FROM '.$this->tableName.' WHERE user_id=:id');
  10. $sql->bindValue(':id', $userId, PDO::PARAM_INT);
  11. $sql->execute();
  12. $row = $sql->fetch($this->fetch_mode);
  13. $this->editUsers  = $row['edit_users'];
  14. $this->editCategories  = $row['edit_categories'];
  15. $this->editPortalContent= $row['edit_portal_content'];
  16. }
  17. public function getUsers(){
  18. return $this->editUsers;
  19. }
  20. public function getPortalContent(){
  21. return $this->editPortalContent;
  22. }
  23. public function getCategories(){
  24. return $this->editCategories;
  25. }
  26. }
  27. ?>


Jeżeli chcę sprawdzić czy user jest zalogowany $user->getLogged();
jeżeli chce sprawdzić czy ma prawa do edycji kategorii $user->permissions->getCategories();

I teraz mam 2 pytania.
1. Czy taka logika, jaką stworzyłem jest w ogóle racjonalna, czyli czy dobrze myślę jeżeli nie, to gdzie robię błędy?
2. PDO ma opcję zwrócenia wyniku jako obiekt. Czy da się automatycznie zapisać atrybuty zwróconego zapytania (czyli pola tabeli) do atrybutów danej klasy?
W chwili obecnej robię to tak:
  1. <?php
  2. $this->editUsers  = $row['edit_users'];
  3. $this->editCategories  = $row['edit_categories'];
  4. $this->editPortalContent= $row['edit_portal_content'];
  5. ?>



Dziękuję serdecznie za wszelką pomoc, a także za opinie na temat samej klasy


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 14.08.2025 - 01:51