The core idea of ​​implementing chain operations in PHP_PHP tutorial

WBOY
Release: 2016-07-13 09:49:19
Original
865 people have browsed it

The core idea of ​​PHP implementing chain operation

This article mainly introduces the core idea of ​​PHP implementing chain operation. This article focuses on explaining its core idea, which is more intuitive. Friends in need can refer to it

Implementation of PHP chain operation

The code is as follows:

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

Create Database.php under Common.

The core of the chain operation is: return $this;

at the end of the method

Database.php:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

namespace Common;

class Database{

function where($where){

return $this; //链式方法最核心的地方在于:在每一个方法之后 return $this

}

function order($order){

return $this;

}

function limit($limit){

return $this;

}

}

1

2

3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

define('BASEDIR',__DIR__); //定义根目录常量

include BASEDIR.'/Common/Loader.php';

spl_autoload_register('\Common\Loader::autoload');

$db = new CommonDatabase();

//传统的操作需要多行代码实现

//$db->where('id = 1');

//$db->where('name = 2');

//$db->order('id desc');

//$db->limit(10);

 

//使用链式操作,一行代码解决问题

$db->where('id = 1')->where('name = 2')->order('id desc')->limit(10);

4

5
6 7

89 10 11 12 13 14
namespace Common;<🎜> <🎜> <🎜> <🎜>class Database{<🎜> <🎜>function where($where){<🎜> <🎜>return $this; //The core of the chain method is: return $this<🎜> after each method <🎜>}<🎜> <🎜>function order($order){<🎜> <🎜>return $this;<🎜> <🎜>}<🎜> <🎜>function limit($limit){<🎜> <🎜>return $this;<🎜> <🎜>}<🎜> <🎜>}<🎜> <🎜>
<🎜>index.php:<🎜> <🎜> ?<🎜>
<🎜>1<🎜> <🎜>2<🎜> <🎜>3<🎜> <🎜>4<🎜> <🎜>5<🎜> <🎜>6<🎜> <🎜>7<🎜> <🎜>8<🎜> <🎜>9<🎜> <🎜>10<🎜> <🎜>11<🎜> <🎜>12<🎜> <🎜>13<🎜> <🎜>14<🎜> <🎜>15<🎜> <🎜> <🎜> <🎜>define('BASEDIR',__DIR__); //Define root directory constants<🎜> <🎜>include BASEDIR.'/Common/Loader.php';<🎜> <🎜>spl_autoload_register('\Common\Loader::autoload');<🎜> <🎜> <🎜> <🎜>$db = new CommonDatabase();<🎜> <🎜> <🎜> <🎜>//Traditional operations require multiple lines of code to implement<🎜> <🎜>//$db->where('id = 1'); //$db->where('name = 2'); //$db->order('id desc'); //$db->limit(10); //Use chain operations to solve the problem with one line of code $db->where('id = 1')->where('name = 2')->order('id desc')->limit(10);
When using chain operations, ide (netbeans will give automatic prompts): http://www.bkjia.com/PHPjc/1020269.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1020269.htmlTechArticleThe core idea of ​​PHP implementing chain operation This article mainly introduces the core idea of ​​PHP implementing chain operation. Focus on explaining its core ideas, which is more intuitive. Friends in need can refer to it...
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