码迷,mamicode.com
首页 > Web开发 > 详细

php 链式语法

时间:2018-03-21 13:47:41      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:调用   fun   cti   esc   div   blog   desc   csharp   arp   

思路:链式语法就是对象调用方法执行赋值操作后返回一个对象,那就可以接着调用方法,最后一个方法返回具体sql语句

<?php
class Sql{
    private $sql=array("from"=>"",
            "where"=>"",
            "order"=>"",
            "limit"=>"");

    public function from($tableName) {
        $this->sql["from"]="FROM ".$tableName;
        return $this;
    }

    public function where($_where=‘1=1‘) {
        $this->sql["where"]="WHERE ".$_where;
        return $this;
    }

    public function order($_order=‘id DESC‘) {
        $this->sql["order"]="ORDER BY ".$_order;
        return $this;
    }

    public function limit($_limit=‘30‘) {
        $this->sql["limit"]="LIMIT 0,".$_limit;
        return $this;
    }
    public function select($_select=‘*‘) {
        return "SELECT ".$_select." ".(implode(" ",$this->sql));
    }
}

$sql =new Sql();

echo $sql->from("testTable")->where("id=1")->order("id DESC")->limit(10)->select();
//输出 SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10
?>
  

  

php 链式语法

标签:调用   fun   cti   esc   div   blog   desc   csharp   arp   

原文地址:https://www.cnblogs.com/zxqblogrecord/p/8616079.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!