PHP小白到大牛直播班+Linux服务器(前30名)
视频教程分类
推荐视频教程
  • php程序员小白到大牛三个月集训php程序员小白到大牛三个月集训
  • Laravel 9 学习正当时—保姆级教程,想学不会都难!Laravel 9 学习正当时—保姆级教程,想学不会都难!
  • 千万级数据并发解决方案(理论+实战)千万级数据并发解决方案(理论+实战)
  • Laravel基础与实战(模块化)Laravel基础与实战(模块化)
  • UNI-APP开发(仿饿了么)UNI-APP开发(仿饿了么)
  • 首页 >后端开发 >php教程 > 正文

    PHP面向对象之标识对象

    原创2018-02-11 11:31:221286 关注公众号:每天精选资源文章推送
    本文主要为大家带来一篇简单谈谈PHP面向对象之标识对象。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,希望能帮助到大家。

    标识对象模式

    这个模式主要功能就是创建sql语句中的wehre条件字符串的,下面直接看代码和注释:

    namespace woo\mapper;
    
    
    //字段对象
    class Field {
      protected $name = null;     //字段名称
      protected $operator = null;     //操作符  
      protected $comps = array();     //存放条件的数组  
      protected $incomplete = false;   //检查条件数组是否有值
      
      function __construct ($name){
        $this->name= $name;
      }
      
      //添加where 条件
      function addTest($operator,$value){
        $this->comps[] = array('name'=>$this->name,'operator'=>$operator,'value'=>$value);
      }
      
      //获取存放条件的数组
      function getComps(){
        return $this->comps;
      }
      
      function isIncomplete(){
        return empty($this->comps);
      }
    }
    
    
    //标识对象
    class IdentityObject {
      protected $currentfield = null;    //当前操作的字段对象
      protected $fields = array();    //字段集合
      private $and = null;
      private $enforce = array();      //限定的合法字段    
      
      function __construct($field = null, array $enforce = null){
        if(!is_null($enforce)){
          $this->enforce = $enforce;
        }
        if(!is_null($field)){
          $this->field($field);
        }
      }
      
      //获取限定的合法字段
      function getObjectFields(){
        return $this->enforce;
      }
      
      //主要功能为设置当前需要操作的对象
      function field($fieldname){
        if(!$this->isVoid()&& $this->currentfield->isIncomplete()){
          throw new \Exception("Incomplete field");
        }
        $this->enforceField($fieldname);
        if(isset($this->fields[$fieldname]){
          $this->currentfield = $this->fields[$fieldname];
        } else {
          $this->currentfield = new Field($fieldname);
          $this->fields[$fieldname] = $this->currentfield;
        }
        return $this;          //采用连贯语法
      }
      
      //字段集合是否为空
      function isVoid(){
        return empty($this->fields);
      }
      
      //检查字段是否合法
      function enforceField ($fieldname){
        if(!in_array($fieldname,$this->enforce) && !empty($this->enforce)){
          $forcelist = implode(',',$this->enforce);
          throw new \Exception("{$fieldname} not a legal field {$forcelist}");
        }
      }
      
      
      //向字段对象添加where条件
      function eq($value){
        return $this->operator("=",$value);
      }
      
      function lt($value){
        return $this->operator("<",$value);
      }
      
      function gt($value){
        return $this->operator(">",$value);
      }
      
      //向字段对象添加where条件
      private function operator($symbol,$value){
        if($this->isVoid){
          throw new \Exception("no object field defined");
        }
        $this->currentfield->addTest($symbol,$value);
        return $this;                   //采用连贯语法
      }
      
      //获取此类中所有字段对象集合的where条件数组
      function getComps(){
        $ret = array();
        foreach($this->fields as $key => $field){
          $ret = array_merge($ret,$field->getComps());
        }
        return $ret;
      }
    }
    
    //客户端代码
    $idobj = new IdentityObject ();
    $idobj->field("name")->eq("The Good Show")->field("start")->gt(time())->lt(time()+(24*60*60));
    $test = $idobj->getComps();
    var_dump($test);
    
    //输出类似下面的内容
    
    /*
    array{
      array('name'=>'name','operator'=>'=','value'=>'The Good Show'),
      array('name'=>'start','operator'=>'>','value'=>'123456'),  //123456表示time()函数输出的时间戳
      array('name'=>'start','operator'=>'<','value'=>'123456')
    }
    
    */

    相关推荐:

    关于JavaScript面向对象的新认识

    javascript面向对象之定义成员方法实例分析

    php面试题中面向对象的题目分析

    以上就是PHP面向对象之标识对象的详细内容,更多请关注php中文网其它相关文章!

    PHP小白到大牛线上直播班第二十期

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。

  • 相关标签:php 对象 标识
  • 相关文章

    相关视频


    专题推荐