PHP development frameworks like thinkPHP will encapsulate the operation classes of the database, and methods will be called, as shown in the following code:
<code>M('Test')->where(['status'=>1])->field('id,name')->select();</code>
What is the idea behind this implementation method? Is there any corresponding specific technical term?
PHP development frameworks like thinkPHP will encapsulate the operation classes of the database, and methods will be called, as shown in the following code:
<code>M('Test')->where(['status'=>1])->field('id,name')->select();</code>
What is the idea behind this implementation method? Is there any corresponding specific technical term?
Chained calls are implemented by returning $this
.
Write a demo for you:
<code class="php"><?php /** * 简单的加减法计算类 * * @author Flc <2016-11-23 19:32:26> */ class Calc { /** * 结果 * @var integer */ protected $result = 0; /** * 加法 * @param number $value */ public function add($value) { $this->result += $value; return $this; } /** * 减法 * @param number $value */ public function sub($value) { $this->result -= $value; return $this; } /** * 返回结果 * @return [type] [description] */ public function result() { return $this->result; } } $calc = new Calc; echo $calc ->add(1) ->add(2) ->sub(1) ->add(11) ->result();</code>
The key point is that each method’s <code>return $this;</code>
You can learn about the principles of the MVC model.
Then let’s take a look at class encapsulation.
M('Test') is actually equivalent to defining a TestModel class.
M('Test')->where(), calls the where method in this class.
The main difficulty lies in the implementation of the MVC model.
This is a tutorial on MVC model implementation, you can check it out.
https://www.shiyanlou.com/cou...
Chain call, return $this is ok, you can refer to various ORM frameworks
Is it a chain operation? Each time an object is returned
Chain operation, return $this, method call
Chain operation
<code>return $this;</code>
Everyone above is right. I'll make a connection.
<code>http://blog.csdn.net/zhengwish/article/details/51742880</code>
Chain calls return objects every time