CodeIgniter連貫操作的底層原理分析
本文分析了CodeIgniter連貫操作的底層原理。分享給大家供大家參考,具體如下:
php oop連貫操作原理
->符號其實是傳遞對象指針的?;蛟S這么說是不對的。
但是,我們可以這么的理解。
不多說。放代碼。
普通用法:
<?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
所以下面的操作可以連續(xù)起來。
試著去掉每個操作里的
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();
?>
更多關(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è)計入門教程》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。
- CodeIgniter生成靜態(tài)頁的方法
- CodeIgniter記錄錯誤日志的方法全面總結(jié)
- CodeIgniter基于Email類發(fā)郵件的方法
- CodeIgniter分頁類pagination使用方法示例
- Codeigniter中集成smarty和adodb的方法
- CodeIgniter針對數(shù)據(jù)庫的連接、配置及使用方法
- CodeIgniter表單驗證方法實例詳解
- CodeIgniter配置之a(chǎn)utoload.php自動加載用法分析
- CodeIgniter自定義控制器MY_Controller用法分析
- CodeIgniter鉤子用法實例詳解
- CodeIgniter多語言實現(xiàn)方法詳解
- CodeIgniter輔助之第三方類庫third_party用法分析
- CodeIgniter常用知識點小結(jié)
相關(guān)文章
php while循環(huán)得到循環(huán)次數(shù)
在for循環(huán)中,我們很容易得到循環(huán)次數(shù),因為是作為條件出現(xiàn)的。在while也可以得到,如下:2013-10-10
yii2實現(xiàn) "上一篇,下一篇" 功能的代碼實例
在很多頁面上都需要加入上一篇,下一篇 按鈕,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-02-02
PHP中使用mpdf 導(dǎo)出PDF文件的實現(xiàn)方法
這篇文章主要介紹了PHP中使用mpdf 導(dǎo)出PDF文件的實現(xiàn)方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下2018-10-10
網(wǎng)頁游戲開發(fā)入門教程二(游戲模式+系統(tǒng))
網(wǎng)頁游戲開發(fā)入門教程二(游戲模式+系統(tǒng))2009-11-11

