Home  >  Article  >  Backend Development  >  How PHP implements the principle of chain operation

How PHP implements the principle of chain operation

*文
*文Original
2018-01-02 14:06:371258browse

How does PHP implement chain operations? Today I will bring you a detailed explanation of the principle of chain operation in PHP. Share it with everyone and give it as a reference. I hope to be helpful.

There are multiple methods in a class. When you instantiate this class and call the methods, you can only call them one by one, similar to:

db.php

<?php

class db
{
    public function where()
    {
    //code here
    }
    public function order()
    {
    //code here
    }
    public function limit()
    {
    //code here
    }
}

index.php

<?php

$db = new db();

$db->where();
$db->order();
$db->limit();

If you want to implement chain calls, you need to add return $ at the end of the method This is enough.

db.php

<?php

class db
{
    public function where()
    {
    //code here
    return $this;
    }
    public function order()
    {
    //code here
    return $this;
    }
    public function limit()
    {
    //code here
    return $this;
    }
}

index.php

<?php

$db = new db();

$db->where()->order()->limit();

Related recommendations:

PHP’s underlying operating mechanism and principles

How does php calculate IP Address mask

How does php generate mysql data dictionary

The above is the detailed content of How PHP implements the principle of chain operation. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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