Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: klasa zmiana stanu
Forum PHP.pl > Forum > PHP > Object-oriented programming
emeres1
Witam,
chciałbym ,aby funkcja setState zmieniała stan w getStateName ,ale nie moge tego ugryźć.Podpowie ktoś jak ma wyglądać ta klasa?

  1. <?php
  2. class Lib_State_Context{
  3.  
  4. static public $_state;
  5. private $stateName;
  6.  
  7. function __construct(){
  8. $state = new Lib_State_Context();
  9. $this->setState(App_State_Guest::getStateName());
  10. }
  11.  
  12. public function setState($stateMame){
  13.  
  14. $this->stateName =$stateName;
  15. $state->getStateName();
  16.  
  17. }
  18.  
  19. public static function getStateName(){
  20. return self::$_state;
  21.  
  22. }
  23. }
  24. ?>
  25.  

  1. <?php
  2.  
  3. class App_State_Guest{
  4.  
  5. public function getStateName() {
  6. return 'Guest';
  7. }
  8.  
  9. }
  10.  
  11. ?>
c3zi
Radzę nauczyć się podstaw OOP.

Przykład (jeżeli faktycznie ta zmienna ma być statyczna):

  1.  
  2. class Alfa
  3. {
  4. private static $_bState = false;
  5.  
  6.  
  7. public static function setState($bValue)
  8. {
  9. self::$_bState = $bValue;
  10. }
  11.  
  12. public static function getState()
  13. {
  14. return self::$_bState;
  15. }
  16. }
  17.  
  18.  
  19. .....
  20. print(Alfa::getState(); // false
  21. Alfa::setState(true);
  22. print(Alfa::getState()); // true
  23.  

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.