[首次分析]PHP写框架

index.php

<?php

/* @Author HLZ
 * @Time 2016年8月3日 15:00:35
 * @Description 框架价格分析
 * 初始目录 分三个部分     
 * ①index.php         
 *       入口文件设置,且统一命名空间
 *          a.设置常量
 *               define("HLZ_PATH",realpath(__DIR__));//获取入口目录位置
 *               define("Mysql帐号密码");
 *          b.引入自己写的Model、View、Controller基类
 * ②App文件夹   分四个文件夹
 *      a.供引入的类库
 *      b.Controller
 *      c.View
 *      d.Logic
 *          要求:其命名空间与类名恰好能找到文件对应.class.php文件
 *      统一自动引入机制,与框架搭建过程中常用的方法,详见下文
 * */

define("HLZ_PATH",realpath(__DIR__."\\.."));//这里只有一个文件,我就当作只有Controller目录吧

//Start.注意:这个是文件加载的方法,只能放在类外面
function __autoload($className){        //使用前提,命名空间得是 根,即namespace \;
                                        //Start:当实例化,对应的类,不存在于该php文件时,自动调用
    $file_where = HLZ_PATH."/".$className.'.class.php';
    $file_where=str_replace('\\',"/",$file_where);//转义成可以require的格式
    if(file_exists($file_where)){
        require_once "$file_where";
    }
    echo "已经require $file_where";
}


//a.接口类
interface test{               //1.1.定义接口,interface
                              //接口算是一个抽象类,但是接口中每个被抽象的方法,都必须被继承的类,全部实现
    function verify($username);//验证用户名
    function info();//验证信息
}
//b.接口逻辑类
class hlz_vip implements test{ //1.2.对应接口,实现,implements
    private $string="VIP Interface Test!";
    public function verify($username){
        if($username==""){
            return  '您未填写用户名';
        }
        return  "Hello ".$username."!";
    }
    public function info(){
        return  $this->string; 
    }
}
//c.普通类
class show_info{
    private $username;
    public function show($username){
        $this->username= $username;
        $user_interface= new hlz_vip(); //2.1.1接口的调用
        $msg['Status'] = $user_interface->verify($username);
        $msg['Info']    = $user_interface->info();
        echo json_encode($msg);
        return $this;       //3.0.链式调用,连贯操作,常应用于数据库操作
    }
    public function say(){
        echo "<h3>链式调用   成功!</h3>";

    }
    public function __construct(){
                             //3.1.当实例化对象时,自动调用,【可以用与类名一样的函数来赖实现】
        echo "<h3>对象创建  成功!</h3><br/>";
    }
    public function __call($function_name, $args){ 
                             //4.1.当调用方法不存在时,自动调用
        echo "<h5>你所调用的函数:$function_name ,在该对象中不存在!</h5>";
        echo "<small>你刚刚输入的参数为:<br/>";
        print_r($args);
        echo "</small>";
    }
    public function __set($property_name, $value) {
                             //5.1.当变量不存在与对象中  或者  变量为该对象的私有属性的时,自动调用
        return $this->$property_name = $value;
    }
    public function __get($property_name) {
                             //6.1.当变量不存在与对象中  或者  变量为该对象的私有属性的时,自动调用
        return isset($this->$property_name) ? $this->$property_name : null;
    }
    public function __isset($property_name){
                 //7.1.在类外部使用 isset() 函数来测定对象里面的私有成员是否被设定时,自动调用
        return isset($this->$property_name);
    }
}
$test = new show_info();
$test->show("HLZ")->say();//测试1:链式调用
$test->hlz("参数一","参数二");//测试2:这个函数不存在于对象中
$test= new \API\HLZ();      //测试3:未初始化引入对应类文件

HLZ.class.php

<?php
namespace API;
echo "自己加载成功";
class  HLZ{
    function __construct(){
        echo "test类调用成功!";
    }
}

自动引入文件的问题

之前在调用的页面,没给命名空间,我们是用到的

function __autoload($className){
    require "$className";
}

办到的

可是,平时我们写框架的时候,加上命名空间是必须的

所以这里有一个新的方法由此而生

可以使用函数 spl_autoload_register 
来注册一个类中的方法来代替 __autoload

具体如下

class require_file{
    static function load( $className ) {//一定得是静态函数
        $file_where = HLZ_PATH."/".$className.'.class.php';
        $file_where=str_replace('\\',"/",$file_where);//转义成可以require的格式
        if(file_exists($file_where)){
            require_once "$file_where";
        }
        echo "已经require $file_where";
    } 
}
//登记自动加载的函数  
    //实例化对象时,如果没有对应的类
    //命名空间的名称、自动加载的类名、类名对应的静态方法
spl_autoload_register( array(__NAMESPACE__."\\require_file","load") ); 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值