PHP class coherent operation

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

 

Guess you like

Origin www.cnblogs.com/phper8/p/11446953.html