连贯操作浅析

简介:这是连贯操作浅析的详细页面,介绍了和php,有关的知识、技巧、经验,和一些php源码等。

class='pingjiaF' frameborder='0' src='http://biancheng.dnbcw.info/pingjia.php?id=337543' scrolling='no'>基本思想:通过PHP的__call方法把要操作的内容保存起来,最后一步操作时执行。

知道这个思想,很快就可以搭建出这样的程序:

<?php
class Test{
	protected $options = array();
	public function __call($method,$args){
		if(in_array($method,array('where','data','add'))){
			$this->options[] = $args[0];
		}else{
			echo '您所操作的方法'.$method.'不存在';
		}
	}
	public function display(){
		return $this->options;
	}
}
$Test = new Test();
$result = $Test->where('a=1,b=2')->display();
var_dump($result);
?>

输出的结果:


再返回看一下ThinkPHP的源码,发现它在每执行一次方法都会有一句: return $this;

把对象自身返回,这才是连贯操作里至关紧要的一步,把自身返回后,执行下一步操作时才不会出现执行方法而找不到对象的问题了。

爱J2EE关注Java迈克尔杰克逊视频站JSON在线工具

http://biancheng.dnbcw.info/php/337543.html pageNo:9
原文地址:https://www.cnblogs.com/ooooo/p/2247022.html