Home >php教程 >php手册 >PHP implements the principle of chain operation

PHP implements the principle of chain operation

WBOY
WBOYOriginal
2016-09-19 08:55:151117browse

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<br /><br />class db<br />{<br />    public function where()<br />    {<br />        //code here<br />    }<br />    public function order()<br />    {<br />        //code here<br />    }<br />    public function limit()<br />    {<br />        //code here<br />    }<br />}

index.php

<?php<br /><br />$db = new db();<br /><br />$db->where();<br>$db->order();<br>$db->limit();

If you want to implement chained calls, just add return $this at the end of the method.

db.php

<?php<br /><br />class db<br />{<br />    public function where()<br />    {<br />        //code here<br />        return $this;<br />    }<br />    public function order()<br />    {<br />        //code here<br />        return $this;<br />    }<br />    public function limit()<br />    {<br />        //code here<br />        return $this;<br />    }<br />}

index.php

<?php<br /><br />$db = new db();<br /><br />$db->where()->order()->limit();

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