Home > Backend Development > PHP Tutorial > php framework - a tip about PHP class calling

php framework - a tip about PHP class calling

WBOY
Release: 2023-03-03 08:18:01
Original
1019 people have browsed it

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>
Copy after login
Copy after login

What is the idea behind this implementation method? Is there any corresponding specific technical term?

Reply content:

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>
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

Everyone above is right. I'll make a connection.

<code>http://blog.csdn.net/zhengwish/article/details/51742880</code>
Copy after login

Chain calls return objects every time

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template