本文实例讲述了php链式操作的实现方式。分享给大家供大家参考,具体如下:
类似$db->where("id=1")->limit("5")->order("id desc")
,链式操作的实现方式
先讲下方法的常规调用;
namespace com; class database{ function where($where){ echo $where; } function order($order){ echo $order; } function limit($limit){ echo $limit; } }
调用
$db = new \com\database(); $db->where(); $db->limit();
缺点:实现多个方法需要多行调用;
链式操作,在方法返回return $this;
即可使用链式操作;
namespace com; class database{ function where($where){ echo $where; return $this; } function order($order){ echo $order; return $this; } function limit($limit){ echo $limit; return $this; } }
使用链式调用:
$db = new \com\database(); $db->where("id=1")->limit("5")->order("id desc");
如对本文有疑问, 点击进行留言回复!!
如何去设计前端框架能力?星巴克消息开放项目从0到1,从点到面的思考
网友评论