Home >Backend Development >PHP Tutorial >Consecutive operations and chained operation examples implemented by PHP, PHP chained examples_PHP tutorial

Consecutive operations and chained operation examples implemented by PHP, PHP chained examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:23:16863browse

Continuous operation and chain operation examples implemented by PHP, PHP chain examples

The coherent operation in PHP looks really cool, and it is very convenient to read the code. Of course, it must be used in OOP. In procedural programs, there is no need to use this method. There is a useful _CALL to implement this method, but the example I wrote below does not use _call. You can expand it.

The SQL statement combination class written below is mainly used for learning. If any students want to use it, please improve it.

/*
 * SQL语句组合实例类,始发文章web开发笔记
 * 学习用,非专业类
 * */
class sql{
	private $sql=array("from"=>"",
			"where"=>"",
			"order"=>"",
			"limit"=>"");
 
	public function from($tableName) {
		$this->sql["from"]="FROM ".$tableName;
		return $this;
	}
 
	public function where($_where='1=1') {
		$this->sql["where"]="WHERE ".$_where;
		return $this;
	}
 
	public function order($_order='id DESC') {
		$this->sql["order"]="ORDER BY ".$_order;
		return $this;
	}
 
	public function limit($_limit='30') {
		$this->sql["limit"]="LIMIT 0,".$_limit;
		return $this;
	}
	public function select($_select='*') {
		return "SELECT ".$_select." ".(implode(" ",$this->sql));
	}
}
 
$sql =new sql();
 
echo $sql->from("testTable")->where("id=1")->order("id DESC")->limit(10)->select();
//输出 SELECT * FROM testTable WHERE id=1 ORDER BY id DESC LIMIT 0,10

ThinkPHP Development Guide-How to realize the coherent operation of the model

It’s very simple, first you need to understand the difference between D() and M().

Here is a simple example:
$model = M('user_list');

$model->where('user_name = "Hello"')->select ();

and

$model->where('user_name = "Hello"');

$model->limit(5)-> ;select();

are the same, mainly when end functions such as select() find() findAll() appear, the language will be executed, otherwise it can be assembled all the time.

is located at

Lib/Think/Core/Model.class.php

PHP static class problem How to operate static classes coherently, such as coherent operations of dynamic classes $x->a()->b()->c(); (I hope someone can give me some advice )

You are perverted. Static classes are usually wrappers of function methods. A method does one thing.

The coherent operation of dynamic classes is just to change the return value of the method to $this. It operates on a class. properties.

Static classes generally do not operate the attributes of the class

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/840758.htmlTechArticle Examples of coherent operations and chained operations implemented in PHP, php chained examples The coherent operations in PHP look really good Cool, and very convenient to read the code. Of course, it must be used in OOP,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn