认真学习php面向对象-6

来源:互联网 发布:mac恢复出厂系统版本 编辑:程序博客网 时间:2024/06/05 20:26

认真学习php面向对象-6

前言

准备写一个认真学习php面向对象的系列,使用php来做网页,没有深入了解php的话,可能三板斧就够了,并不需要有多高深!如有错误,欢迎各位不吝赐教!进度安排的话,我学到哪里,就更新到哪里了!形式的话就采用一个需求小案例,然后实现,并附上自己的总结,文章源码

需求 :1)模拟框架模板展示display方法显示view页面

上一节中,我们成功创建了路由资源文件并正确访问了我们定义的方法,那么这一节我们模拟框架常用的方display方法展示view页面。

创建login.html

这里写图片描述
在入口文件中,改写路由规则,强迫每个函数都有一个默认参数display,此处我们使用了匿名函数

index.php

<?php      function getMatch($v) {          return preg_match('/[a-zA-Z]+/',$v);      }      $display=function($tpl){            require (getcwd().'/vars');            include (getcwd().'/page/'.$tpl.'.html');      };      $pi=$_SERVER['PATH_INFO'];      $pi=isset($_SERVER["PATH_INFO"])?$_SERVER["PATH_INFO"]:false;      if(!$pi) exit('404');      $route=require ("request_route");      $route_keys=array_keys($route);      foreach ($route_keys as $key) {          $new_key=str_replace('/','\/',$key);          if (preg_match('/'.$new_key.'/',$pi,$result)) {                  $route_obj=$route[$key];                  if ($route_obj['RequestMethod']==$_SERVER['REQUEST_METHOD']) {                      $className=$route_obj['Class'];                      $method=$route_obj['Method'];                      require (getcwd().'/code/'.$className.".class.php");                      $params=array_filter($result,'getMatch',ARRAY_FILTER_USE_KEY);                      $class_obj=new ReflectionClass($className);                      $getMethod=$class_obj->getMethod($method);                      $params['display']=$display;                      $getMethod->invokeArgs($class_obj->newInstance(),$params);//                      if($params && count($params)>0) {//                          $getMethod->invokeArgs($class_obj->newInstance(),$params);//                      } else {//                          $getMethod->invoke($class_obj->newInstance());//                      }                  } else {                      exit('not allowed!');                  }          }      }

Index.class.php

 /**     * @RequestMapping("/login",Method=GET)     */    public function login($display) {        $display('login');    }

记得写完之后,需要编译一下,生成路由资源文件,并且开启php内置服务器,访问
这里写图片描述
这里写图片描述

效果

这里写图片描述

原创粉丝点击