PHP class coherent operation implementation method, php class
Coherent operations in PHP look really cool and very convenient for reading code. Of course, they must be used in OOP. In Procedural programs, this method is not necessary. We can use _ CALL to implement this method. The example I wrote below does not use _ call. You can extend it.
The SQL statement combination class written below is mainly used for learning. If you want to use it, please complete it.
/*
* SQL statement combination instance class, originating Article web Development notes
* Www.chhua.com
* Used for 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
Free reprint, please note:Reprinted from WEB Development notes www.chhua.com
Link:Implementation Method http://www.chhua.com/web-note3446 of PHP class coherent operation