源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP实现链式操作的原理详解

  • 时间:2021-11-16 10:47 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP实现链式操作的原理详解
在一个类中有多个方法,当你实例化这个类,并调用方法时只能一个一个调用,类似: [b]db.php[/b]
<?php

class db
{
public function where()
{
//code here
}
public function order()
{
//code here
}
public function limit()
{
//code here
}
} 
[b]index.php[/b]
<?php

$db = new db();

$db->where();
$db->order();
$db->limit(); 
如果要实现链式调用,这要在方法的结束添加return $this即可。 [b]db.php[/b]
<?php

class db
{
public function where()
{
//code here
return $this;
}
public function order()
{
//code here
return $this;
}
public function limit()
{
//code here
return $this;
}
} 
[b]index.php[/b]
<?php

$db = new db();

$db->where()->order()->limit();
以上这篇PHP实现链式操作的原理详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程素材网。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部