This article mainly introduces the coherent and chained operation instances implemented by PHP. This article uses a database operation class as an example. For more information, see
This article mainly introduces the coherent and chained operation instances implemented by PHP. This article uses a database operation class as an example. For more information, see
Coherent operations in PHP look really cool and very convenient for reading code. Of course, they must be used in OOP. In Procedural programs, there is no need to use this method. 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 * for learning, non-professional class **/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 ". $ _ 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