Implementation method of PHP class coherent operation
The coherent operation in PHP looks really cool, and it is 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 _CALL to implement this method, but the example I wrote below does not use _call. You can expand it.
The SQL statement combination class written below is mainly for learning. If any students want to use it, please improve it.
/*
* SQL statement combination instance class, the original article web development notes
* www.chhua.com
* For learning purposes, 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
http://www.bkjia.com/PHPjc/998011.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/998011.htmlTechArticle Implementation method of PHP class coherent operation The coherent operation in PHP looks really cool, and it is also very convenient to code. Reading, of course, must be used in OOP. In procedural programs, just...