CodeIgniter連貫操作的底層原理分析
本文分析了CodeIgniter連貫操作的底層原理。分享給大家供大家參考,具體如下:
php oop連貫操作原理
->符號(hào)其實(shí)是傳遞對(duì)象指針的?;蛟S這么說是不對(duì)的。
但是,我們可以這么的理解。
不多說。放代碼。
普通用法:
<?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();
?>
看到了沒有。
連起來了??梢园巡僮鞔饋?。
看起來直觀多了。閱讀代碼時(shí)也輕松了很多。
類里面操作都返回了一個(gè)指針。
$this.
他等價(jià)于你初始化的那個(gè)對(duì)象 $oktest
所以下面的操作可以連續(xù)起來。
試著去掉每個(gè)操作里的
return $this
你將會(huì)看到錯(cuò)誤提示。
例子:
<?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();
?>
更多關(guān)于CodeIgniter相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《ThinkPHP入門教程》、《ThinkPHP常用方法總結(jié)》、《Zend FrameWork框架入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對(duì)大家基于CodeIgniter框架的PHP程序設(shè)計(jì)有所幫助。
- CodeIgniter生成靜態(tài)頁的方法
- CodeIgniter記錄錯(cuò)誤日志的方法全面總結(jié)
- CodeIgniter基于Email類發(fā)郵件的方法
- CodeIgniter分頁類pagination使用方法示例
- Codeigniter中集成smarty和adodb的方法
- CodeIgniter針對(duì)數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter表單驗(yàn)證方法實(shí)例詳解
- CodeIgniter配置之a(chǎn)utoload.php自動(dòng)加載用法分析
- CodeIgniter自定義控制器MY_Controller用法分析
- CodeIgniter鉤子用法實(shí)例詳解
- CodeIgniter多語言實(shí)現(xiàn)方法詳解
- CodeIgniter輔助之第三方類庫third_party用法分析
- CodeIgniter常用知識(shí)點(diǎn)小結(jié)
相關(guān)文章
基于PHP實(shí)現(xiàn)生成隨機(jī)水印圖片
這篇文章主要介紹了基于PHP實(shí)現(xiàn)生成隨機(jī)水印圖片,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-12-12
php while循環(huán)得到循環(huán)次數(shù)
在for循環(huán)中,我們很容易得到循環(huán)次數(shù),因?yàn)槭亲鳛闂l件出現(xiàn)的。在while也可以得到,如下:2013-10-10
yii2實(shí)現(xiàn) "上一篇,下一篇" 功能的代碼實(shí)例
在很多頁面上都需要加入上一篇,下一篇 按鈕,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02
PHP中使用mpdf 導(dǎo)出PDF文件的實(shí)現(xiàn)方法
這篇文章主要介紹了PHP中使用mpdf 導(dǎo)出PDF文件的實(shí)現(xiàn)方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
網(wǎng)頁游戲開發(fā)入門教程二(游戲模式+系統(tǒng))
網(wǎng)頁游戲開發(fā)入門教程二(游戲模式+系統(tǒng))2009-11-11

