像thinkPHP的PHP开发框架都会封装数据库的操作类,会出现方法调用方法的情况,如下代码:
M('Test')->where(['status'=>1])->field('id,name')->select();
这种实现方式是什么思想呢,有没有对应的具体技术名词呢?
像thinkPHP的PHP开发框架都会封装数据库的操作类,会出现方法调用方法的情况,如下代码:
M('Test')->where(['status'=>1])->field('id,name')->select();
这种实现方式是什么思想呢,有没有对应的具体技术名词呢?
写个demo给你:
<?php
/**
* 简单的加减法计算类
*
* @author Flc <2016-11-23 19:32:26>
*/
class Calc
{
/**
* 结果
* @var integer
*/
protected $result = 0;
/**
* 加法
* @param number $value
*/
public function add($value)
{
$this->result += $value;
return $this;
}
/**
* 减法
* @param number $value
*/
public function sub($value)
{
$this->result -= $value;
return $this;
}
/**
* 返回结果
* @return [type] [description]
*/
public function result()
{
return $this->result;
}
}
$calc = new Calc;
echo $calc
->add(1)
->add(2)
->sub(1)
->add(11)
->result();
重点就是每个方法的
return $this;
MVC模型的原理可以去了解一下。
然后再去看下关于类的封装。
M('Test')其实相当于定义了一个 TestModel 的类.
M('Test')->where(),则是调用该类里面的where方法。
主要的难点还是在于对MVC模型的实现吧。
这个是关于MVC模型实现的一个教程,可以去看一下。
https://www.shiyanlou.com/cou...
1 回答4.1k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
2 回答2.2k 阅读✓ 已解决
1 回答1.4k 阅读✓ 已解决
2 回答2.2k 阅读
1 回答579 阅读✓ 已解决
1.1k 阅读
链式调用, 通过返回
$this
实现.