Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Użycie private czy public?
Kerth
post
Post #1





Grupa: Zarejestrowani
Postów: 250
Pomógł: 1
Dołączył: 6.08.2012

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


Cześć,
przeglądam tą stronę:
http://phpedia.pl/wiki/Model_View_Controller

zawarty jest tam kod:


  1. <?php
  2.  
  3. /**
  4.  * klasa modelu
  5.  */
  6. class Model {
  7.  
  8. public $tytul;
  9. public $naglowek;
  10. public $tresc;
  11.  
  12. public function pobierzTytul()
  13. {
  14. return $this->tytul;
  15. }
  16.  
  17. public function pobierzNaglowek()
  18. {
  19. return $this->naglowek;
  20. }
  21.  
  22. public function pobierzTresc()
  23. {
  24. return $this->tresc;
  25. }
  26.  
  27. }
  28.  
  29. /**
  30.  * klasa widoku
  31.  */
  32. class View {
  33.  
  34. private $tytul;
  35. private $naglowek;
  36. private $tresc;
  37.  
  38. public function __construct($tytul, $naglowek, $tresc)
  39. {
  40. $this->tytul = $tytul;
  41. $this->naglowek = $naglowek;
  42. $this->tresc = $tresc;
  43. $this->wyswietlStrone();
  44. }
  45.  
  46. private function wyswietlStrone()
  47. {
  48. echo '<html>';
  49. echo '<head>';
  50. echo '<title>'.$this->tytul.'</title>';
  51. echo '</head>';
  52. echo '<body>';
  53. echo '<h1>'.$this->naglowek.'</h1>';
  54. echo '<p>'.$this->tresc.'</p>';
  55. echo '</body>';
  56. echo '</html>';
  57. }
  58.  
  59. }
  60.  
  61. /**
  62.  * klasa kontrolera
  63.  * obsługuje model oraz uruchamia widok
  64.  */
  65. class Controller {
  66.  
  67. // obiekty
  68. private $model;
  69. private $view;
  70.  
  71. // dane
  72. private $tytul;
  73. private $naglowek;
  74. private $tresc;
  75.  
  76.  
  77. public function __construct()
  78. {
  79. $this->uruchomModel();
  80. $this->ustawDane();
  81. $this->pobierzDane();
  82. $this->uruchomWidok();
  83. }
  84.  
  85. private function uruchomModel()
  86. {
  87. $this->Model = new Model;
  88. }
  89.  
  90. private function ustawDane()
  91. {
  92. $this->Model->tytul = 'Tytuł';
  93. $this->Model->naglowek = 'Nagłówek';
  94. $this->Model->tresc = 'Treść';
  95. }
  96.  
  97. private function pobierzDane()
  98. {
  99. $this->tytul = $this->Model->pobierzTytul();
  100. $this->naglowek = $this->Model->pobierzNaglowek();
  101. $this->tresc = $this->Model->pobierzTresc();
  102. }
  103.  
  104. private function uruchomWidok()
  105. {
  106. $this->View = new View($this->tytul, $this->naglowek, $this->tresc);
  107. }
  108. }
  109.  
  110. // uruchomienie prostego wzorca MVC
  111.  
  112. new Controller();
  113.  
  114. ?>


Dlaczego w warstwie modelu ustawione jest:

public $tytul;
public $naglowek;
public $tresc;

a w warstwie widoku i kontrolera:

private $tytul;
private $naglowek;
private $tresc;

? Może mi ktoś to wyjaśnić? Po co 3 razy definiować to samo?
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: 3.10.2025 - 22:25