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