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();
?>
看到了沒有。
連起來了??梢园巡僮鞔饋怼?/p>
看起來直觀多了。閱讀代碼時也輕松了很多。
類里面操作都返回了一個指針。
$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)框架進階教程》、《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)文章
thinkphp實現(xiàn)發(fā)送郵件密碼找回功能實例
這篇文章主要介紹了thinkphp實現(xiàn)發(fā)送郵件密碼找回功能的方法,以實例形式詳細(xì)講述了配置文件與功能代碼的實現(xiàn)方法,是非常實用的技巧,需要的朋友可以參考下2014-12-12
php小型企業(yè)庫存管理系統(tǒng)的設(shè)計與實現(xiàn)代碼
雙專業(yè)畢業(yè)論文做了一個小型企業(yè)庫存管理系統(tǒng),使用PHP&mysql組合進行開發(fā)。頭一次開發(fā)稍微大一點的東西,php也是自學(xué)的2011-05-05
Laravel中validation驗證 返回中文提示 全局設(shè)置的方法
今天小編就為大家分享一篇Laravel中validation驗證 返回中文提示 全局設(shè)置的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09
在WordPress的文章編輯器中設(shè)置默認(rèn)內(nèi)容的方法
這篇文章主要介紹了在WordPress的文章編輯器中設(shè)置默認(rèn)內(nèi)容的方法,包括給不同類型的文章設(shè)置不同內(nèi)容的具體方法,需要的朋友可以參考下2015-12-12

