PHP Implementation of Coherent Operation Chain Operation Example

  • 2021-07-07 06:49:45
  • OfStack

Coherent operation in PHP looks really cool, and it is also very convenient to read the code. Of course, it must be used in OOP. In procedural programs, there is no need to use this method. There is a useful implementation of this method _ CALL to achieve, and I write this example, is not with _ call, we can expand 1 under it.

The SQL statement combination class written below is mainly used for learning. If there are students who want to use it, please improve it again.


/*
 * SQL Statement combination instance class, originating article web Development notes 
 *  Learning, non-professional 
 * */
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();
// Output  SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10


Related articles: