Fatal error: Call to a member function display() on null 怎么解決??代碼都貼出來了
index.php代碼
<?php
//url形式 index.php?controller=控制器&method=方法名
require_once('function.php'); //帶引號才行
$controllerAllow=array('test','index');
$methodAllow=array('test','index','show');
$controller=in_array($_GET['controller'],$controllerAllow)?daddslashes($_GET['controller']):'index';
$method=in_array($_GET['method'],$methodAllow)?daddslashes($_GET['method']):'index';
C($controller,$method);
?>
function.php代碼
<?php
function C($name,$method){ //C控制器縮寫大 ,方法里面可以傳參數,所以方法可以在傳參
require_once('/libs/Controller/'.$name.'Controller.class.php'); ?//引入控制器文件
eval('$obj=new '.$name.'Controller();$obj->'.$method.'();'); //eval()函數調用簡單但是不安全
}
C('test','show');
function M($name){ ?//M模型縮寫大 ,方法里面不可以傳參數
require_once('/libs/Model/'.$name.'Model.class.php');
/* $testModel=new testModel();//控制器 -> 按指令選取一個合適的模型
$data=$testModel->get(); //模型 ? -> 按控制器指令取相應數據 */
eval('$obj=new '.$name.'Model();');
return $obj;
}
function V($name){
require_once('/libs/View/'.$name.'View.class.php');
eval('$obj=new '.$name.'View();');
}
function daddslashes($str){ ? //daddslashes()用來轉義非法字符 函數返回在預定義字符之前添加反斜杠的字符串。
return(!get_magic_quotes_gpc())?addslashes($str):$str;
}
?>
testController.class.php代碼
<?php
class testController{
function show(){//控制器的作用是調用模型,并調用視圖。將模型產生的數據傳遞給視圖,并讓相關視圖去顯示
$testModel=M('test');?
$data=$testModel->get(); ? ?
$testView=V('test'); ??
$testView->display($data); ?
}
}
?>
2018-05-31
謝謝?云彩無色3804005?貼出代碼
2018-03-20
首先你要確定,沒加index.php之前,訪問你的代碼function.php你的代碼沒有錯誤,如果有,說明是你的幾個函數哪里寫錯了,如果沒有,說明是index.php寫錯了。然后你在看你傳入的controller和method,先打印出你傳入的controller和method,再看看有沒有問題。
2018-03-19
這個事說你的對象沒有display方法,我看你是view調用的display方法,看看testView.class.php中你對testView這個class定義display這個方法了嗎