search
Homephp教程php手册PHP实现的连贯操作、链式操作实例,php链式实例

PHP实现的连贯操作、链式操作实例,php链式实例

PHP中的连贯操作看起来的确很酷,也非常的方便代码的阅读,当然了必须是在OOP中用才行,在过程化的程序中,就没有必要用这种方法了。有实现这个方法的有用_CALL来实现的,而我下面写的这个例子,则不是用_call的,大家可以扩展一下吧。

下面写的这个SQL语句组合类,主要是用于学习的,如果有同学想拿去用,请再完善一下。

/*
 * 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开发指南-模型之连贯操作怎实现的

很简单,首先你要明白D()和M()的区别。

我这里简单举例:
$model = M('user_list');

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



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

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

是一样的,主要是以select() find() findAll() 这类结束函数出现时,语言才会被执行,否则可以一直组装。

位置在

Lib/Think/Core/Model.class.php
 

php静态类问题 静态类怎连贯操作 如动态类的连贯操作 $x->a()->b()->c(); (希望有鬼才指点)

你变态啊 静态类 一般都是 函数方法的包装 一个方法做一件事

动态类的连贯操作,只是将方法的返回值改为$this而已,操作的是一个类的属性。

静态类一般是不操作类的属性的
 

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.