Fatal error: Call to undefined function M()

来源:4-5 [MVC实例]入口文件功能

qq_dd_5

2018-03-20 15:54

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

写回答 关注

7回答

  • qq_戕斖_0
    2018-06-13 12:00:19

    33234

  • qq_dd_5
    2018-03-22 22:34:02

    https://img1.mukewang.com/5ab3beca0001bbf512420241.jpg

    变成这个问题了

  • qq_dd_5
    2018-03-22 22:29:59

    <?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;

       }


    ?>


  • 云彩无色3804005
    2018-03-21 14:28:31

    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;

        }

    ?>


    qq_dd_... 回复云彩无色38...

    hao

    2018-03-23 19:26:18

    共 4 条回复 >

  • 云彩无色3804005
    2018-03-21 14:28:05

    undefined function M() 出现这个问题是因为你的大括号闭合问题,你C()函数的括号内包含了M()和C()函数,你后面还会出现错误,M()和V()函数实例化时候new后面要加空格再跟类名,你这两处都没加,还有一处是V()函数最后是要返回值用return $obj;   你试下,我测试


    qq_dd_...

    你好 我回复晚了

    2018-03-22 22:34:24

    共 1 条回复 >

  • qq_dd_5
    2018-03-20 16:18:16
    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方法

       


    ?>



  • 云彩无色3804005
    2018-03-20 16:05:51

    提示错误是未定义M()函数,你再仔细检查下 

    qq_dd_... 回复云彩无色38...

    怎么样呢

    2018-03-20 18:42:43

    共 4 条回复 >

MVC架构模式分析与设计

通过学习MVC理论知识,由浅入深带您实现人生第一个MVC框架

82364 学习 · 929 问题

查看课程

相似问题