Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Using $this when not in object context - ale $this jest w klasie. :/
Meares
post
Post #1





Grupa: Zarejestrowani
Postów: 102
Pomógł: 13
Dołączył: 16.10.2009
Skąd: Łódź

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


  1. class Controller_Core {
  2. function view($ViewName, $ViewData = array()) {
  3. if(empty($ViewName)) return;
  4. extract($ViewData, EXTR_SKIP);
  5. include('mainview.html');
  6. }
  7. }
  8.  
  9. class Main_Controller extends Controller_Core {
  10. function index() {
  11. $this->view('mainview.html');
  12. }
  13. }

Cytat
Fatal error: Using $this when not in object context.

Powyższy błąd występuje w jedynej linijce z $this ($this->view('mainview.html');), w kodzie podanym powyżej.
Ogólnie błąd wydaje się dziwny i nie na miejscu, w końcu używam tego $this w klasie. -.- Wie ktoś jaka jest przyczyna problemu?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Meares
post
Post #2





Grupa: Zarejestrowani
Postów: 102
Pomógł: 13
Dołączył: 16.10.2009
Skąd: Łódź

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


Ehh...
  1. class Vihroll {
  2. private $qs;
  3. private $segments;
  4. private $controller;
  5. private $action;
  6. private $arguments;
  7. public $nextclass;
  8.  
  9. public function __construct() {
  10. $this->qs = $_SERVER['QUERY_STRING'];
  11. $this->segments = $this->_GetUriSegments($this->qs);
  12.  
  13. if(!empty($this->segments[0])) {
  14. $this->controller = ucfirst(array_shift($this->segments)).'_'.ucfirst('controller');
  15. } else {
  16. $this->controller = 'Main_Controller';
  17. array_shift($this->segments);
  18. }
  19.  
  20. if(!empty($this->segments[0])) {
  21. $this->action = array_shift($this->segments);
  22. } else {
  23. $this->action = 'index';
  24. array_shift($this->segments);
  25. }
  26.  
  27. $this->arguments = $this->segments;
  28. call_user_func_array(array($this->controller, $this->action), $this->arguments);
  29. }
  30.  
  31. private function _GetUriSegments($qs) {
  32. $qs = explode('/', $qs);
  33. return $qs;
  34. }
  35. }


1. Tak, na pewno.
2. Powinna być jeszcze deklaracja klasy ($class = new Main_Controller();), ale od tego mam __autoload();.
3. Jeśli dam w Main_Controller::index(); echo 'x'; zamiast $this->view('mainview.html'); to hula i ten x się wyświetla.

Ten post edytował Meares 5.01.2010, 03:36:11
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
4 Użytkowników czyta ten temat (4 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 8.10.2025 - 00:45