2016-03-25 11 views
-1

私のクライアントのウェブサイトに何か奇妙なエラーがあります。誰かが彼のために作ったコードです。エラーメッセージは、$thisがオブジェクトコンテキストではないが、Classが拡張されていると言っています(Appから)。

助けてください。

エラー

Fatal error: Using $this when not in object context in contactController.php on line 6

contactController.php

class contactController extends App{  
    public function index(){ 
     $this->view('content'); //error mmessage is pointing here 
    } 
} 

app.php

class App{ 

    public $controller; 
    public $method; 
    public $params = []; 

    public function view($file){ 
     include(site_path() . '/views/' . $file . '.php'); 
    } 

    public function model($file){ 
     require_once(site_path() . '/models/' . $file . '.php'); 
    } 

    public function engine(){ 
     global $core_current_ControllerMethod, $core_current_controller, $core_current_method; 
     //Get the current controller and method from a helper function 
     $get_wc_cm = get_wc_cm(); 
     //Assign it to the global variable 
     $core_current_ControllerView = $get_wc_cm; 
     //Seperate the controller and method 
     $cm = explode('@', $get_wc_cm); 
     $controller = !empty($cm[0])? $cm[0]: null; // This is the controller 
     $method = !empty($cm[1])? $cm[1]: null; // This is the method 
     //Assign it to the global varaible 
     $core_current_controller = $controller; 
     $core_current_method = $method; 
     //Assign it to the class variable 
     $this->controller = $controller; 
     $this->method = $method; 

     $ControllerFile = site_path(). '/controllers/' . $this->controller . '.php'; 
     if(file_exists($ControllerFile)){ 
      require_once($ControllerFile); 
      new $this->controller; 
      $callback = is_callable(array($this->controller, $this->method), false); 
      if($callback){ 
       call_user_func_array([$this->controller, $this->method], [$this->params]); 
      } 
     } 
    } 

} 

$app = (new App)->engine(); 
+0

が、それは方法がそのコンストラクタから呼び出されていることだろうか? – arkascha

+0

TBH、卵を産む前にコードベース全体を火で浄化する必要があります –

+0

@Martinそれは重複していません。私の質問はあなたが参考にしている答えとは異なっています。 –

答えて

1

変更しよう:

class contactController extends App{  
    public function index(){ 
     $this->view('content'); //error mmessage is pointing here 
    } 
} 

へ:

class contactController extends App{  
    public function index(){ 
     parent::view('content'); //error mmessage is pointing here 
    } 
} 
+0

まず、エラーがスローされ、_何か他のことが起こる前に、理解していないでしょうか?プログラミングは推測ゲームではありません。 – arkascha

+0

@Pierre your solution works。なぜそれが起こったのか説明できるなら、いいですね。できれば。 –

+0

違いは、親がそれを継承するのではなく、独自の実装を持っている場合、$ this->が子の実装を呼び出す間、parent ::が親の実装を呼び出すことです。アイデアは、子クラスが存在しない親クラスで再実装されたメソッドを探すのを避けることです。 子クラスにビューメソッドがある場合、$ thisを使用できます。 – Pierre

関連する問題