Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Wiele obiektów i jedna zmienna.
Forum PHP.pl > Forum > Przedszkole
EdeusEx
mam problem, ale pierw przedstawie skrypty:

game.php
  1. <?php
  2. require_once('mysql.php');
  3. require_once('player.php');
  4. require_once('website.php');
  5. class Game {
  6. function Game($getPlayerName) {
  7. $this->PlayerName = $getPlayerName;
  8. $this->SQL = new MySQL($this->PlayerName);
  9. $this->Player = new Player($this->PlayerName);
  10. $this->Website = new Website($this->PlayerName);
  11. }
  12. }
  13. ?>


Najważniejszy jest player.php:
  1. <?php
  2. class Stats extends MySQL {
  3. function getPercent($stat, $precision=0) {
  4. $getStat = mysql_query(&#092;"SELECT * FROM \".$this->prefix(0, \"player_stats\").\" WHERE user='\".$this->PlayerName.\"'\");
  5. $_Stat = mysql_fetch_assoc($getStat);
  6. @$countPC = round(($_Stat[$stat]/$_Stat[$stat.'_max'])*100, $precision);
  7.  
  8. return $countPC;
  9. }
  10.  
  11. function getValue($stat) {
  12. $getStat = mysql_query(&#092;"SELECT * FROM \".$this->prefix(0, \"player_stats\").\" WHERE user='\".$this->PlayerName.\"'\");
  13. $_Stat = mysql_fetch_assoc($getStat);
  14. return $_Stat[$stat];
  15. }
  16.  
  17. function changeValue($stat) {
  18.  
  19. }
  20. }
  21.  
  22.  
  23. class Player extends MySQL {
  24. function Player() {
  25. $this->EQ = new Equipment($this->PlayerName);
  26. $this->Stats = new Stats($this->PlayerName);
  27. $this->Account = new Account($this->PlayerName);
  28. $this->Messages = new Messages($this->PlayerName);
  29. $this->Guild = new Guild($this->PlayerName);
  30. }
  31. }
  32. ?>


mysql.php
  1. <?php
  2. class MySQL {
  3. public $PlayerName = &#092;"\";
  4.  
  5.  
  6. function __construct($getPlayerName) {
  7. if ($this->isConnected==false) {
  8. if (@!mysql_connect($this->db_host, $this->db_user, $this->db_pass)) {
  9. die(&#092;"0\");
  10. } else {
  11. $this->isConnected = true;
  12. }
  13. }
  14. $this->PlayerName = $getPlayerName;
  15. }
  16. }
  17. ?>



I index.php, gdzie jest deklaracja:
  1. <?php
  2. require_once('includes/game.php');
  3.  
  4. if (!isset($_SESSION['member'])) {
  5. $setPlayerName = 'guest';
  6. } else {
  7. $setPlayerName = $_SESSION['member'];
  8. }
  9.  
  10. $Game = new Game($setPlayerName);
  11. ?>

Dlaczego, wywołując $Game->Player->Stats->getValue('blabla'); $this->PlayerName nie ma żadnej wartości? Gdzieś zgubiłem przekazywanie ^.-
Kicok
Tu zgubiłeś przekazywanie:
  1. <?php
  2.  
  3.  
  4. $this->Player = new Player($this->PlayerName);
  5.  
  6. // (...)
  7.  
  8. class Player extends MySQL {
  9. function Player() {
  10.  
  11.  
  12. ?>



Masz całkowicie skopaną strukturę klas.
Nawet jeśli klasa MySQL byłaby przydatna - a nie jest bo i tak stosujesz mysql_query - to bezsensem jest dziedziczenie po niej wszystkich innych klas. Zainteresuj się wzorcem Singleton albo przerób MySQL na klasę statyczną i w ogóle nie rób obiektów tej klasy.

Poza tym zmienna $playerName powinna być raczej w klasie Player, a nie MySQL.


Nie wspominając już o mieszaniu klas w stylu PHP4 z klasami w stylu PHP5...
EdeusEx
Co znaczy, ze mieszam klasy php4 z php5 ? To sa jakies standardy dla kazdej z wersji ? ^.-
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.