PHP类连贯操作

<?php
/**
 * Modular:
 * Created: PhpStorm.
 * User: dbag
 * Date: 2019/9/2
 * Time: 15:06
 */
class Demo {
   protected $table='';
   protected $where='';
   protected $field='*';
   public function where($where="1=1"){
        if($this->where){
            $this->where .= ' and '.$where;
        }else{
            $this->where= $where;
        }
       return $this;
   }
   public function table($table){
       $this->table=$table;
       return $this;
   }
   public function field($field='*'){
       $this->field = $field;
       return $this;
   }
   public function find(){
       $sql = "select {$this->field} from  {$this->table} where {$this->where} limit 1";
       return $sql;
   }
}
 $db  = new Demo();
 $sql = $db->field('id,username,realname')
            ->where("id=123")
            ->where("is_del=1")
            ->table('users')
            ->find();
echo $sql;
# select id,username,realname from users where id=123 and is_del=1 limit 1

 

posted @ 2019-09-02 15:38  大智如蠢  阅读(304)  评论(0编辑  收藏  举报