Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Poprawność konstruktora?
Nortonek
post
Post #1





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 29.11.2003

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


Witam

W tej chwili konstruktor wymusza 3 połączenie do bazy, czy taki zapis konstruktora jest poprawny, czy można to zoptymalizować?

  1. <?php
  2.  
  3. $id = 1;
  4.  
  5. $Player = new Player($id);
  6.  
  7. $USER['nick'] = $Player->nick;
  8. $USER['level'] = $Player->level;
  9. $USER['praca'] = $Player->praca;
  10. $USER['kasa'] = $Player->kasa;
  11.  
  12. echo $USER['kasa'];
  13.  
  14.  
  15.  
  16.  
  17. class Players {
  18. public $nick;
  19. public $level;
  20. public $id_players;
  21. public $kasa;
  22. public $praca;
  23.  
  24.  
  25. function __construct($id) {
  26. $this -> id_players= $id;
  27. self::get_dane();
  28. self::update_kasa();
  29. self::get_dane();
  30. }
  31.  
  32. public function get_dane(){
  33. global $db, $table_players;
  34.  
  35. $stats = $db -> Execute("SELECT * FROM $table_players WHERE `id_players`='".$this -> id_players."' AND `aktywny`='1' ");
  36. $this -> nick = $stats -> fields['nick'];
  37. $this -> level= $stats -> fields['level'];
  38. $this -> kasa= $stats -> fields['kasa'];
  39. $this -> praca= $stats -> fields['praca'];
  40. $stats -> Close();
  41. }
  42.  
  43. public function update_kasa(){
  44. global $db, $table_players;
  45.  
  46. if ( $this -> praca <= time() ) {
  47. $kasa = 100;
  48. $result = $db -> Execute("UPDATE $table_players SET `kasa`= `kasa` + '$kasa', `praca`='0' WHERE `id_players`='".$this -> id_players."' ");
  49. $result -> Close();
  50. }
  51. }
  52.  
  53.  
  54. }
  55.  
  56. ?>
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Nortonek
post
Post #2





Grupa: Zarejestrowani
Postów: 50
Pomógł: 0
Dołączył: 29.11.2003

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


Wymyśliłem sobie że update ma wykonywać się przy każdym odświeżeniu strony,

podczas gry często się zmienia stan zmiennych dla Playera

Po wykonaniu update pokazuje stan zmiennych ...

Dodałem do klasy warunek, który zmniejsza o 1 zapytanie do bazy, zaczynam od zera OOP to pierwsze kroki i dlatego tak się dopytuję czy dobrze kombinuję

  1. <?php
  2.  
  3. $id = 1;
  4.  
  5. $Player = new player($id);
  6.  
  7. $USER['nick'] = $Player->nick;
  8. $USER['level'] = $Player->level;
  9. $USER['praca'] = $Player->praca;
  10. $USER['kasa'] = $Player->kasa;
  11.  
  12. echo $USER['kasa'];
  13.  
  14.  
  15.  
  16.  
  17. class Players {
  18. public $nick;
  19. public $level;
  20. public $id_players;
  21. public $kasa;
  22. public $praca;
  23. public $byl_update; /* DODANO */
  24.  
  25.  
  26. function __construct($id) {
  27. $this->byl_update = 1; /* DODANO */
  28. $this -> id_players= $id;
  29. self::get_dane();
  30. self::update_kasa();
  31. if ($this->byl_update==2) self::get_dane(); /* DODANO */
  32. }
  33.  
  34. public function get_dane(){
  35. global $db, $table_players;
  36.  
  37. $stats = $db -> Execute("SELECT * FROM $table_players WHERE `id_players`='".$this -> id_players."' AND `aktywny`='1' ");
  38. $this -> nick = $stats -> fields['nick'];
  39. $this -> level= $stats -> fields['level'];
  40. $this -> kasa= $stats -> fields['kasa'];
  41. $this -> praca= $stats -> fields['praca'];
  42. $stats -> Close();
  43. }
  44.  
  45. public function update_kasa(){
  46. global $db, $table_players;
  47.  
  48. if ( $this -> praca <= time() ) {
  49. $kasa = 100;
  50. $result = $db -> Execute("UPDATE $table_players SET `kasa`= `kasa` + '$kasa', `praca`='0' WHERE `id_players`='".$this -> id_players."' ");
  51. $result -> Close();
  52. $this->byl_update = 2; /* DODANO */
  53. }
  54. }
  55.  
  56.  
  57. }
  58.  
  59. ?>




Ten post edytował Nortonek 12.02.2014, 21:18:45
Go to the top of the page
+Quote Post

Posty w temacie


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

 



RSS Aktualny czas: 11.10.2025 - 17:35