Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Singleton wartość static ma 2 wartosci?
Forum PHP.pl > Forum > Przedszkole
newicz
Witam,

Jestem potocznie nazywanym w Internecie "noobem" smile.gif, jednak przyszło mi pisać drobny skrypcik na potrzeby własne, prosty CMS jednak jest problem po krótce kod:

Login.class.php
  1. <?php
  2.  
  3. class Login
  4. {
  5. private static $instance;
  6. public $userStatus;
  7.  
  8. private function __construct() { $this -> setAnonymous(); }
  9.  
  10. public function setAnonymous ()
  11. {
  12. $this -> userStatus = 'anonymous';
  13. }
  14.  
  15. public function setUser ()
  16. {
  17. $this -> userStatus = 'user';
  18. }
  19.  
  20. public function setAdmin ()
  21. {
  22. $this -> userStatus = 'admin';
  23. }
  24.  
  25. public function checkStatus ()
  26. {
  27. return $this -> userStatus;
  28. }
  29.  
  30. // Singleton
  31.  
  32. public static function singleton()
  33. {
  34. if (!isset(self::$instance))
  35. {
  36. $c = __CLASS__;
  37. self::$instance = new $c;
  38. }
  39. return self::$instance;
  40. }
  41.  
  42. public function __clone()
  43. {
  44. trigger_error('no clone', E_USER_ERROR);
  45. }
  46. }
  47.  
  48.  
  49. ?>


index.php
  1. <?php
  2.  
  3.  
  4. require_once ( 'classes/MySql.class.php' );
  5. require_once ( 'classes/Login.class.php' );
  6. require_once ( 'classes/Templates.class.php' );
  7.  
  8. require_once ( 'functions/Configuration.php' );
  9. require_once ( 'functions/Helpers.php' );
  10.  
  11. // Singletons
  12. $MySQL = Mysql::singleton();
  13. $Login = Login::singleton();
  14. // ----- end
  15.  
  16. // set Configuration
  17. $Data = niceURL();
  18. // ----- end
  19.  
  20. // View Core
  21. $coreTpl = new Template();
  22.  
  23. $coreArray = array (
  24.  
  25. "menu" => Menu ($Login -> checkStatus(), $MySQL),
  26. "content" => Content ($Data, $MySQL, $Login),
  27.  
  28. "url_template" => __url( $MySQL ).'templates',
  29. "footer" => 'copyright'
  30. );
  31.  
  32. echo $coreTpl -> change ( $coreArray, 'Login.tpl', 'mtLogin' );
  33. // ----- end
  34.  
  35.  
  36. ?>


funkcja Contents()
  1. function Content ( $Data, $MySQL, $Login )
  2. {
  3. $userStatus = $Login -> checkStatus();
  4.  
  5. if ( !empty ( $Data[0] ) )
  6. {
  7. $Answer = $MySQL -> qr ("SELECT * FROM epmenu WHERE name='$Data[0]'");
  8.  
  9. if (($Answer['access'] == 'admin' || $Answer['access'] == 'user') and $userStatus == 'anonymous')
  10. {
  11. $tmp = 'Access denied';
  12. }
  13. elseif ( empty ( $Answer['id'] ) )
  14. {
  15. $tmp = '404';
  16. }
  17. else
  18. {
  19. switch ( $Answer['name'] )
  20. {
  21.  
  22. // ANONYMOUS
  23. case 'Login':
  24. $tmp = cnLogin ( $MySQL, $Login );
  25. //if ( $tmp == 'Success' ) header ('Location: '.__url( $MySQL ).'index.php/Successfull/1');
  26. //if ( $tmp == 'Error' ) header ('Location: '.__url( $MySQL ).'index.php/Error/1');
  27. break;
  28.  
  29. case 'Forgot':
  30. $tmp = '1';
  31. break;
  32.  
  33.  
  34. // USER
  35. case 'Undone':
  36. $tmp = '1';
  37. break;
  38.  
  39. case 'Progress':
  40. $tmp = '';
  41. break;
  42.  
  43. case 'Done':
  44. $tmp = '1';
  45. break;
  46.  
  47. case 'Docs':
  48. $tmp = '1';
  49. break;
  50.  
  51. case 'Upload':
  52. $tmp = '';
  53. break;
  54.  
  55. case 'Profile':
  56. $tmp = '1';
  57. break;
  58.  
  59.  
  60. // ADMINISTRATION
  61. case 'Administration':
  62. $tmp = '1';
  63. break;
  64.  
  65.  
  66. // SYTUATIONS
  67. case 'Successfull':
  68. $Success = new Template();
  69.  
  70. switch ( $Data[1] )
  71. {
  72. case '1':
  73. $message = 'Congratulations! You have successful log in!';
  74. $back = __url( $MySQL ).'index.php/';
  75. $url = __url( $MySQL );
  76. break;
  77. }
  78.  
  79. $tmp = $Success -> change ( array ( 'message' => $message, 'back' => $back, 'url' => $url ), 'Success.tpl', 'mtScreen' );
  80. break;
  81.  
  82. case 'Error':
  83. $Error = new Template();
  84.  
  85. switch ( $Data[1] )
  86. {
  87. case '1':
  88. $message = 'Login error: username or password is wrong!';
  89. $back = __url( $MySQL ).'index.php/Login';
  90. $url = __url( $MySQL );
  91. break;
  92. }
  93.  
  94. $tmp = $Error -> change ( array ( 'message' => $message, 'back' => $back, 'url' => $url ), 'Error.tpl', 'mtScreen' );
  95. break;
  96. }
  97. }
  98.  
  99. return $tmp;
  100. }
  101. }
  102. // ----- end


Funkcja cnLogin
  1. function cnLogin ( $MySQL, $Login )
  2. {
  3. $username = $_POST['username'];
  4. $password = $_POST['password'];
  5.  
  6. if ($Login -> checkStatus != 'anonymous') header ('Location: '.__url( $MySQL ).'index.php/');
  7.  
  8. if ( empty($username) and empty($password) )
  9. {
  10. $Formular = new Template();
  11. $tmp = $Formular -> change ( array('url' => __url( $MySQL )), 'Formular.tpl', 'mtLogin' );
  12. }
  13. else
  14. {
  15. $Number = $MySQL -> nr ("SELECT * FROM epusers WHERE username='$username'");
  16.  
  17. if ( $Number > 0 )
  18. {
  19. $Answer = $MySQL -> qr ("SELECT * FROM epusers WHERE username='$username'");
  20.  
  21. if ( $password == $Answer['password'] )
  22. {
  23. $tmp = 'Success';
  24. if ( $Answer['access'] == 'admin' ) $Login -> setAdmin();
  25. else $Login -> setUser();
  26. }
  27. else
  28. {
  29. $tmp = 'Error';
  30. }
  31. }
  32. else
  33. {
  34. $tmp = 'Error';
  35. }
  36. $tmp = $Login -> checkStatus();
  37. }
  38.  
  39. return $tmp;
  40. }
  41. // ----- end


No właśnie i teraz jak wywołam
  1. $Login -> checkStatus()

To w index.php wartość jest anonymous, jednak w funkcji cnLogin warość jest user (loguje się na użytkownika z takim statusem)

2 róże wartości singletona?questionmark.gif Wiem że singleton to nie _global, no ale chyba nie powinno tak być?
Proszę oświećcie mnie sad.gif

P.S. Prosiłbym o konkretne wypowiedzi, a nie w stylu "naucz się php", czy " po co używasz singletona on ssie"


Z góry dziękuje i pozdrawiam!
wookieb
W cnLogin
Kod
$Login -> checkStatus

na
Kod
$Login -> checkStatus()


Zapewne masz wyłączone pełne wyświetlanie błędów (a jest bardzo przydatne) to nie widziałeś tego błędu.
newicz
Dzięki za zainteresowanie, i wykrycie tego byka, ale niestety to nie zmienia faktu, że nadal są 2 różne wartości i w funkcji jestem zalogowany, ale już w index.php nie... sad.gif
wookieb
Sporo tego kodu. Mój pomysł to wklejenie do każdej twojej metody zmianiającej wartość userStatus tej funkcji ( http://pl.php.net/manual/pl/function.debug...t-backtrace.php )
Uczyń swój userStatus prywatny bo od zwracania jej wartości masz metode, a nóż zabezpieczysz żeby przypadkiem tej wartości nie zmienić.
newicz
Zrobiłem tak jak mi doradziłeś zmieniam wartość userStatus tylko w cnLogin, więc wstawiłem tam:

  1. echo '<pre>';
  2. debug_print_backtrace();
  3. echo '</pre>';


efektem jest:

  1. #0 cnLogin(Mysql Object ([] => localhost,[] => root,[] => root,[] => evoplus), Login Object ([] => anonymous)) called at [C:\Program Files (x86)\WebServ\httpd-users\Evo+\functions\Helpers.php:71]
  2. #1 Content(Array ([0] => Login), Mysql Object ([] => localhost,[] => root,[] => root,[] => evoplus), Login Object ([] => anonymous)) called at [C:\Program Files (x86)\WebServ\httpd-users\Evo+\index.php:27]
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.