PHP实现链式操作的核心思想
发布时间:2020-12-13 02:41:52 所属栏目:PHP教程 来源:网络整理
导读:《PHP实例:PHP实现链式操作的核心思想》要点: 本文介绍了PHP实例:PHP实现链式操作的核心思想,希望对您有用。如果有疑问,可以联系我们。 PHP 链式操作的实现 PHP教程 ? PHP教程 代码如下: ?$db-where()-limit()-order(); ? 在 Common 下创建 Database.ph
《PHP实例:PHP实现链式操作的核心思想》要点: PHP 链式操作的实现PHP教程 ?PHP教程
代码如下:
?$db->where()->limit()->order(); ? 在 Common 下创建 Database.php.PHP教程 链式操作最核心的地方在于:在方法的最后 return $this;PHP教程 Database.php: <?php namespace Common; class Database{ function where($where){ return $this; //链式方法最核心的地方在于:在每一个方法之后 return $this } function order($order){ return $this; } function limit($limit){ return $this; } } index.php:PHP教程 <?php define('BASEDIR',__DIR__); //定义根目录常量 include BASEDIR.'/Common/Loader.php'; spl_autoload_register('CommonLoader::autoload'); $db = new CommonDatabase(); //传统的操作需要多行代码实现 //$db->where('id = 1'); //$db->where('name = 2'); //$db->order('id desc'); //$db->limit(10); //使用链式操作,一行代码解决问题 $db->where('id = 1')->where('name = 2')->order('id desc')->limit(10); 在使用链式操作时,ide(netbeans 会给出自动提示):PHP教程 ? 欢迎参与《PHP实例:PHP实现链式操作的核心思想》讨论,分享您的想法,编程之家 52php.cn为您提供专业教程。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |