首页 >php教程 >php手册 > 正文

PHP实现链式操作的原理

原创2016-09-19 08:55:150464
在一个类中有多个方法,当你实例化这个类,并调用方法时只能一个一个调用,类似:

db.php


class db
{
public function where()
{
//code here
}
public function order()
{
//code here
}
public function limit()
{
//code here
}
}

index.php


$db = new db();

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

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

db.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


$db = new db();

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

php中文网最新课程二维码

声明:本文原创发布php中文网,转载请注明出处,感谢您的尊重!如有疑问,请联系admin@php.cn处理

相关文章

相关视频


网友评论

文明上网理性发言,请遵守 新闻评论服务协议

我要评论
  • 专题推荐

    作者信息

    php中文网

    认证0级讲师

    推荐视频教程
  • javascript初级视频教程javascript初级视频教程
  • jquery 基础视频教程jquery 基础视频教程
  • 视频教程分类