php和js链式操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/daily886/article/details/86598693
<?php 
function p($a){
	echo '<pre>';
	var_dump($a);
	echo '</pre>';
}
class link{
	private $sql = '';
	public function field($field = ''){
		$this->sql .= 'select '.$field;
		return $this;
	}
	public function table($table =''){
		$this->sql .= ' from '.$table;
		return $this;
	}
	public function where($where = ''){
		$this->sql .= ' where = '.$where;
		return $this;
	}
	public function select(){
		return $this->sql;
	}
}

$sql = (new link())->field('*')->table('user')->where('1=1')->select();
p($sql);

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	<a href="" id="a1"></a>
	<a href="" id="a2"></a>
	<p></p>
</body>
</html>
<style>
	a{
		display: inline-block;
		padding: 0px;
		margin-right: 10px;
	}
	#a1{
		border-left: 4px solid transparent;
		border-right: 4px solid transparent;
		border-bottom: 4px solid black;
	}
	#a2{
		border-left: 4px solid transparent;
		border-right: 4px solid transparent;
		border-top: 4px solid black;
	}
</style>
<script>
	let newClass = {
		sql: '',
		field: function(field){
			field = field == undefined ? '' : field //默认值
			this.sql += 'select '+field;
			return this;
		},
		table: function(table){
			table = table == undefined ? '' : table //默认值
			this.sql += ' from '+table;
			return this;
		},
		where: function(where){
			where = where == undefined ? '' : where //默认值
			this.sql += ' where '+where;
			return this;
		},
		select: function(){
			return this.sql;
		}
	}
	let sql = newClass.field('*').table('user').where('1=1').select();
	document.write(sql);
</script>

 

展开阅读全文

没有更多推荐了,返回首页