Home > php教程 > php手册 > body text

PHP implements the principle of chain operation

WBOY
Release: 2016-09-19 08:55:15
Original
1075 people have browsed it

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

index.php

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

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

index.php

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

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template