华域联盟 PHP php链式操作的实现方式分析

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

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《php面向对象程序设计入门教程》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

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

您可能感兴趣的文章:

本文由 华域联盟 原创撰写:华域联盟 » php链式操作的实现方式分析

转载请保留出处和原文链接:https://www.cnhackhy.com/61531.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部