Fatal error: Call to undefined function M() in F:\wamp\www\mvc\libs\Controller\testController.class.php on line 6 这该怎么解决呢,请大神指点
33234

变成这个问题了
<?php
//备注:建立一个控制器调用函数C
function C($name,$method){
require_once('/libs/Controller/'.$name.'Controller.class.php');
//$testController = new testController();
//$testController->show();
//将字符串转化为可执行的php语句
eval('$obj = new '.$name.'Controller();$obj->'.$method.'();');
C('test','show');
}
//传递的参数只有name 因为模型通常带有自己的参数,所以不封装起来。控制器原则不能有自己的参数。
function M($name){
require_once('/libs/Model/'.$name.'Model.class.php');
// $testModel = new testModel();
eval('$obj= new '.$name.'Model();');
return $obj;
}
function V($name){
require_once('/libs/View/'.$name.'View.class.php');
// $testView = new testView();
eval('$obj=new '.$name.'View();');
return $obj;
}
//对输入参数进行筛选,
function daddslashes($str){
return (!get_magic_quotes_gpc())?addslashes($str):$str;
}
?>
function.php
<?php
//备注:建立一个控制器调用函数C
function C($name,$method){
require_once('/libs/Controller/'.$name.'Controller.class.php');
//$testController = new testController();
//$testController->show();
//将字符串转化为可执行的php语句
eval('$obj = new '.$name.'Controller();$obj->'.$method.'();');
//传递的参数只有name 因为模型通常带有自己的参数,所以不封装起来。控制器原则不能有自己的参数。
}
function M($name){
require_once('/libs/Model/'.$name.'Model.class.php');
// $testModel = new testModel();
eval('$obj= new '.$name.'Model();');
return $obj;
}
function V($name){
require_once('/libs/View/'.$name.'View.class.php');
// $testView = new testView();
eval('$obj=new '.$name.'View();');
return $obj;
}
//对输入参数进行筛选,
function daddslashes($str){
return (!get_magic_quotes_gpc())?addslashes($str):$str;
}
?>
undefined function M() 出现这个问题是因为你的大括号闭合问题,你C()函数的括号内包含了M()和C()函数,你后面还会出现错误,M()和V()函数实例化时候new后面要加空格再跟类名,你这两处都没加,还有一处是V()函数最后是要返回值用return $obj; 你试下,我测试
function.php
<?php
//备注:建立一个控制器调用函数C
function C($name,$method){
require_once('/libs/Controller/'.$name.'Controller.class.php');
//$testController = new testController();
//$testController->show();
//将字符串转化为可执行的php语句
eval('$obj = new '.$name.'Controller();$obj->'.$method.'();');
C('test','show');
//传递的参数只有name 因为模型通常带有自己的参数,所以不封装起来。控制器原则不能有自己的参数。
function M($name){
require_once('/libs/Model/'.$name.'Model.class.php');
// $testModel = new testModel();
eval('$obj= new'.$name.'Model();');
return $obj;
}
function V($name){
require_once('/libs/View/'.$name.'View.class.php');
// $testView = new testView();
eval('$obj=new'.$name.'View();');
echo $obj;
}
}
//对输入参数进行筛选,
function daddslashes($str){
return (!get_magic_quotes_gpc())?addslashes($str):$str;
}
?>
testController.php
<?php
class testController{
//控制器的作用是调用模型,并调用视图,将模型产生的数据传递给视图并让相关视图去显示
function show(){
//$testModel = new testModel(); 由于index可以简化为以下代码
$testModel = M('test');
$data = $testModel->get();
//$testView = new testView();
$testView = V('test');
$testView -> display($data);
}
}
?>
testModel.php
<?php
class testModel{
function get(){//模型的作用是获取数据并处理返回数据
return "Hello world";
}
}
?>
testView.php<?php
class testView{
function display($data){//视图的作用是将获得的数据进行组织、美化等,并最终向用户终端输出
echo $data;
}
}
?>
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);
?>
test.php
<?php
require_once('testController.class.php');
require_once('testModel.class.php');
require_once('testView.class.php');
$testController = new testController();//把类实例化
$testController->show();//使用show方法
?>
提示错误是未定义M()函数,你再仔细检查下