设为首页 加入收藏

TOP

PHP实现链式操作的原理
2017-10-10 11:58:06 】 浏览:4837
Tags:PHP 实现 链式 操作 原理

在一个类中有多个方法,当你实例化这个类,并调用方法时只能一个一个调用,类似:

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();

如果要实现链式调用,这要在方法的结束添加return $this即可。

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();

 

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇PHP类的自动载入机制 下一篇PHP开发规范

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目