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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-03-22 我要评论
想了解PHP实现链式操作的原理详解的相关内容吗,在本文为您仔细讲解php链式操作的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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实现链式操作的原理详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们