The consistent operation in PHP looks really cool, it's very handy for code reading, of course, it has to be used in OOP, and in a procedural process, there's no need to use this method. There are useful _call to implement this method, and I write the following example, it is not used _call, we can expand it.
The following write this SQL statement combination class, is mainly used for learning, if there are students want to take to use, please perfect again.
/*
* SQL statement combination instance class, originating article Web development note
* www.chhua.com
* For learning, non-professional class
* */
Class sql{
Private $sql =array ("from" and "",
"WHERE" = "",
"Order" = "" ",
"Limit" and "=");
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 ()->select ();
Output SELECT * from TestTable WHERE id=1 ORDER by ID DESC LIMIT 0,10
Free Reprint, reproduced Please specify: reproduced from the Web development Note www.chhua.com
This article link address: The implementation method of PHP class coherence operation http://www.chhua.com/web-note3446
The implementation method of PHP class coherence operation