Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Problem z dziedziczeniem
Hesto
post 26.11.2013, 19:57:57
Post #1





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.11.2013

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


Witam,
Mam problem z dziedziczeniem. Zrobiłem klasę AbstractController która zawiera metodę getView(), która zwraca mi zmienną $view w której trzymam ścieżkę do pliku z widokiem.

  1. abstract class AbstractController implements AbstractControllerInterface
  2. {
  3. private $view;
  4.  
  5. public function getView()
  6. {
  7. return $this->view;
  8. }
  9. }


Stworzyłem klasy UserController oraz IndexController która dziedziczy po klasie AbstractController. W metodzie tych klas, indexAction() ustawiam ścieżkę do pliku i zapisuję ją w zmiennej $view:

  1. class UserController extends AbstractController
  2. {
  3. private $service;
  4. private static $indexRoute;
  5. private static $registerRoute;
  6. private static $loginRoute;
  7.  
  8. public function __construct()
  9. {
  10. self::$indexRoute = BASE_PATH . '\module\User\view\index.phtml';
  11. }
  12.  
  13. public function indexAction()
  14. {
  15. $this->view = self::$indexRoute;
  16. }
  17.  
  18. }
  19.  
  20. class IndexController extends AbstractController
  21. {
  22. private static $indexRoute;
  23.  
  24. public function __construct()
  25. {
  26. self::$indexRoute = BASE_PATH . '\module\Application\view\index.phtml';
  27. }
  28.  
  29. public function indexAction()
  30. {
  31. return $this->view = self::$indexRoute;
  32.  
  33. }
  34. }


Dalej stworzyłem klasę Strategy która wybiera, który controller utworzyć:

  1. class Strategy
  2. {
  3. private static $strategy;
  4.  
  5. private function __construct() {}
  6.  
  7. public static function getStrategy($id)
  8. {
  9. switch ($id) {
  10. case "index":
  11. self::$strategy = new IndexController();
  12. break;
  13. case "user":
  14. self::$strategy = new UserController();
  15. break;
  16. }
  17. return self::$strategy;
  18. }
  19. }


następnie stworzyłem klasę View która odpowiada za wyświetlenie:

  1. class View
  2. {
  3. private $output;
  4. private $layout;
  5.  
  6. public function __construct($controller)
  7. {
  8. $this->layout = BASE_PATH . '\module\Application\view\layout\layout.phtml';
  9. $this->output = $controller->getView();
  10. require($this->layout);
  11. }
  12.  
  13. public function getLayout()
  14. {
  15. return $this->layout;
  16. }
  17.  
  18. public function getOutput()
  19. {
  20. require($this->output);
  21. }
  22. }


No i na końcu plik index.php który to wszystko łączy:
  1. $url = parse_url($_SERVER['REQUEST_URI']);
  2. $array = explode('/', $url['path']);
  3. var_dump($array);
  4. if ($array[1] == NULL) {
  5. $array[1] = 'index';
  6. }
  7.  
  8. try {
  9. $controller = Strategy::getStrategy($array[1]);
  10. $controller->indexAction();
  11. $view = new View($controller);
  12. }
  13. catch(Exception $e) {
  14. echo $e->getMessage();
  15. }


Wszystko działa pięknie jeśli do klas UserController i IndexController dodam ręcznie metodę getView która znajduje się u ich rodzica AbstractController. Same niestety tej metody nie chcą dziedziczyć. Tzn dziedziczą lecz do klasy View dociera pusta zmienna $view zbłędem:

Warning: require(): Filename cannot be empty in C:\websites\mikrop\vendor\Framework\View\View.php on line 20
Fatal error: require(): Failed opening required '' (include_path='.;C:\php\pear') in C:\websites\mikrop\vendor\Framework\View\View.php on line 20

Tak jakby klasa UserController i IndexController odziedziczyły metodę getView() lecz nie wykonały jej wogóle(?) lub niewykonały jej porawnie? Jeśli bedę miał większą ilość Controllerów to wolałbym aby metoda która i tak zawsze będzie wyglądać tak samo, mogła być dziedziczona. Niestety gdzieś robię błąd. Proszę o pomoc.
Go to the top of the page
+Quote Post
irmidjusz
post 26.11.2013, 22:41:46
Post #2





Grupa: Zarejestrowani
Postów: 279
Pomógł: 60
Dołączył: 25.02.2012

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


Poczytaj o widoczności zmiennych protected i private. W klasie AbstractController właściwość $view jest private, czyli nie jest widoczna w klasach potomnych.


--------------------
there is much to be learned
Go to the top of the page
+Quote Post
Hesto
post 27.11.2013, 08:50:06
Post #3





Grupa: Zarejestrowani
Postów: 11
Pomógł: 0
Dołączył: 14.11.2013

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


No pewnie że o to chodziło! Dziękuje, jedno małe przeoczenie i tyle godzin zmarnowanych tongue.gif.
Go to the top of the page
+Quote Post

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 Wersja Lo-Fi Aktualny czas: 23.04.2024 - 19:31