php链式操作的实现方式分析

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

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

php链式操作的实现方式分析

  2021-04-03 我要评论

本文实例讲述了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");

希望本文所述对大家PHP程序设计有所帮助。

您可能感兴趣的文章:

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

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