CodeIgniter连贯操作的底层原理分析


Posted in PHP onMay 17, 2016

本文分析了CodeIgniter连贯操作的底层原理。分享给大家供大家参考,具体如下:

php oop连贯操作原理

->符号其实是传递对象指针的。或许这么说是不对的。

但是,我们可以这么的理解。

不多说。放代码。

普通用法:

<?php
class test
{
 public $a='';
 public $b='';
 public function actiona() {
  $this->a="hello";
  return $this;
 }
 public function actionb() {
  $this->b="world";
  return $this;
 }
 public function actionc() {
  echo $this->a." ".$this->b;
 }
}
$oktest=new test();
$oktest->actiona();
$oktest->actionb();
$oktest->actionc();
?>

连贯用法:

<?php
class test
{
 public $a='';
 public $b='';
 public function actiona() {
  $this->a="hello";
  return $this;
 }
 public function actionb() {
  $this->b="world";
  return $this;
 }
 public function actionc() {
  echo $this->a." ".$this->b;
 }
}
$oktest=new test();
$oktest->actiona()->actionb()->actionc();
?>

看到了没有。

连起来了。可以把操作串起来。

看起来直观多了。阅读代码时也轻松了很多。

类里面操作都返回了一个指针。

$this.

他等价于你初始化的那个对象 $oktest

所以下面的操作可以连续起来。

试着去掉每个操作里的

return $this

你将会看到错误提示。

例子:

<?php
class sql{
 public $select;
 public $from;
 public $where;
 public $order;
 public $limit;
 public function from($_from='FROM test') {
 $this->from=$_from;
 return $this;
 }
 public function where($_where='WHERE 1=1') {
 $this->where=$_where;
 return $this;
 }
 public function order($_order='ORDER BY id DESC') {
 $this->order=$_order;
 return $this;
 }
 public function limit($_limit='LIMIT 0,30') {
 $this->limit=$_limit;
 return $this;
 }
 public function select($_select='SELECT *') {
 $this->select=$_select;
 return $this->select." ".$this->from." ".$this->where." ".$this->order." ".$this->limit;
 }
}
$sql =new sql();
echo $sql->from()->where()->order()->limit()->select();
?>

希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

PHP 相关文章推荐
用PHP连mysql和oracle数据库性能比较
Oct 09 PHP
php echo()和print()、require()和include()函数区别说明
Mar 27 PHP
PHP内核介绍及扩展开发指南―基础知识
Sep 11 PHP
PHP用星号隐藏部份用户名、身份证、IP、手机号等实例
Apr 08 PHP
PHP 双链表(SplDoublyLinkedList)简介和使用实例
May 12 PHP
5款适合PHP使用的HTML编辑器推荐
Jul 03 PHP
浅谈php的优缺点
Jul 14 PHP
解决nginx不支持thinkphp中pathinfo的问题
Jul 21 PHP
Zend Framework教程之动作的基类Zend_Controller_Action详解
Mar 07 PHP
Zend Framework教程之Bootstrap类用法概述
Mar 14 PHP
laravel migrate初学常见错误的解决方法
Oct 11 PHP
如何用RabbitMQ和Swoole实现一个异步任务系统
May 29 PHP
CI框架常用方法小结
May 17 #PHP
CodeIgniter记录错误日志的方法全面总结
May 17 #PHP
CI框架整合widget(页面格局)的方法
May 17 #PHP
深入剖析浏览器退出之后php还会继续执行么
May 17 #PHP
CI框架出现mysql数据库连接资源无法释放的解决方法
May 17 #PHP
CI框架集成Smarty的方法分析
May 17 #PHP
CI框架中数据库操作函数$this-&gt;db-&gt;where()相关用法总结
May 17 #PHP
You might like
为什么《星际争霸》是测试人工智能的理想战场
2019/12/03 星际争霸
PHP实现异步调用方法研究与分享
2011/10/27 PHP
php防注入及开发安全详细解析
2013/08/09 PHP
PHP实现读取一个1G的文件大小
2013/08/24 PHP
PHP错误Allowed memory size of 67108864 bytes exhausted的3种解决办法
2014/07/28 PHP
PHP的Yii框架的基本使用示例
2015/08/21 PHP
浅谈php和js中json的编码和解码
2016/10/24 PHP
PHP小程序支付功能完整版【基于thinkPHP】
2019/03/26 PHP
document.all与WEB标准
2020/05/13 Javascript
Eval and new funciton not the same thing
2012/12/27 Javascript
jQuery事件绑定与解除绑定实现方法
2015/04/15 Javascript
利用jQuery设计一个简单的web音乐播放器的实例分享
2016/03/08 Javascript
js检查是否关闭浏览器的方法
2016/08/02 Javascript
javascript-解决mongoose数据查询的异步操作
2016/12/22 Javascript
微信小程序 PHP后端form表单提交实例详解
2017/01/12 Javascript
JavaScript代码实现txt文件的上传预览功能
2018/03/27 Javascript
详解Javascript中new()到底做了些什么?
2018/03/29 Javascript
浅谈Node 异步IO和事件循环
2019/05/05 Javascript
跟老齐学Python之玩转字符串(2)更新篇
2014/09/28 Python
python 线程的暂停, 恢复, 退出详解及实例
2016/12/06 Python
名片管理系统python版
2018/01/11 Python
用python实现百度翻译的示例代码
2018/03/09 Python
python 实现得到当前时间偏移day天后的日期方法
2018/12/31 Python
深入浅析Python 中的sklearn模型选择
2019/10/12 Python
Python加密模块的hashlib,hmac模块使用解析
2020/01/02 Python
Python列表切片常用操作实例解析
2020/03/10 Python
Python工程师必考的6个经典面试题
2020/06/28 Python
Trunki英国官网:儿童坐骑式行李箱
2017/05/30 全球购物
高中毕业自我鉴定
2013/12/22 职场文书
工程开工庆典邀请函
2014/02/01 职场文书
先进员工事迹材料
2014/12/20 职场文书
廉洁自律承诺书2015
2015/01/22 职场文书
大学生预备党员自我评价
2015/03/04 职场文书
golang json数组拼接的实例
2021/04/28 Golang
Vue通过懒加载提升页面响应速度
2021/05/10 Vue.js
利用JuiceFS使MySQL 备份验证性能提升 10 倍
2022/03/17 MySQL