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

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 Aktualny czas: 22.08.2025 - 04:18