我在客户端网站上看到一些奇怪的错误。这是别人为他的应用程序编写的代码。错误消息表示$this
不在对象上下文中,但Class
已从App
扩展到where。
请帮忙。
错误
致命错误:在第6行的contactController.php中不在对象上下文中时使用$this
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();
尝试更改:
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
}
}