PHP实现链式操作的核心思想
发布时间:2020-12-12 20:57:19 所属栏目:PHP教程 来源:网络整理
导读:PHP 链式操作的实现 代码如下: where()->limit()->order(); 在 Common 下创建 Database.php。 链式操作最核心的地方在于:在方法的最后 return $this; Database.php: class Database{ function where($where){ return $this; //链式方法最核心的地方在于:在
PHP 链式操作的实现 代码如下: where()->limit()->order();
在 Common 下创建 Database.php。 链式操作最核心的地方在于:在方法的最后 return $this; Database.php: class Database{
function where($where){ return $this; //链式方法最核心的地方在于:在每一个方法之后 return $this } function order($order){ return $this; } function limit($limit){ return $this; } } index.php: $db = new CommonDatabase();
//传统的操作需要多行代码实现 //使用链式操作,一行代码解决问题 在使用链式操作时,ide(netbeans 会给出自动提示): (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |