Home >Backend Development >PHP Tutorial >Consecutive operations and chained operation examples implemented by PHP, PHP chained examples_PHP tutorial
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 used for learning. If any students want to use it, please improve it.
/* * SQL语句组合实例类,始发文章web开发笔记 * 学习用,非专业类 * */ 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(); //输出 SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10
It’s very simple, first you need to understand the difference between D() and M().
Here is a simple example:
$model = M('user_list');
$model->where('user_name = "Hello"')->select ();
and
$model->where('user_name = "Hello"');
$model->limit(5)-> ;select();
are the same, mainly when end functions such as select() find() findAll() appear, the language will be executed, otherwise it can be assembled all the time.
is located at
Lib/Think/Core/Model.class.php
You are perverted. Static classes are usually wrappers of function methods. A method does one thing.
The coherent operation of dynamic classes is just to change the return value of the method to $this. It operates on a class. properties.
Static classes generally do not operate the attributes of the class