php链式操作的实现_php教程,php
PHP fsockopen函数详解_php教程
PHP fsockopen是一个功能比较强大的函数。我们在这篇文章中将会对这个函数做一个具体的介绍,希望对大家有所帮助。
php链式操作的关键是在做完操作后要return $this;
一、不使用__call方法实现链式操作
<?php 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 ?>
二、使用__call方法实现链式操作
关于PHP+jQuery-ui拖动浮动层排序并保存到数据库实例_php教程
PHP+jQuery-ui实现的拖动浮动层排序布局并将拖动后的浮动层位置排序结果保存到数据库实例。
__call()在对象调用一个不可访问的方法时会被触发,所以可以实现类的动态方法的创建,实现php的方法重载功能,但它其实是一个语法糖(__construct()方法也是)。
<?php class String { public $value; public function __construct($str=null) { $this->value = $str; } public function __call($name, $args) { $this->value = call_user_func($name, $this->value, $args[0]); return $this; } public function strlen() { return strlen($this->value); } } $str = new String('01389'); echo $str->trim('0')->strlen(); // 输出结果为 4;trim('0')后$str为"1389" ?>
相关推荐:
PHP视频教程:https://www.ki4.cn/course/list/29/type/2.html
以上就是php链式操作的实现的详细内容,更多请关注ki4网其它相关文章!
开启PHP服务的方法_php教程
PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。本文就来为大家介绍一下开启PHP服务的方法。