CodeIgniter連貫操作的底層原理分析
來源:易賢網(wǎng) 閱讀:726 次 日期:2016-08-22 14:34:16
溫馨提示:易賢網(wǎng)小編為您整理了“CodeIgniter連貫操作的底層原理分析”,方便廣大網(wǎng)友查閱!

本文分析了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();

?>

希望本文所述對大家基于CodeIgniter框架的PHP程序設(shè)計有所幫助。

更多信息請查看網(wǎng)絡(luò)編程
易賢網(wǎng)手機網(wǎng)站地址:CodeIgniter連貫操作的底層原理分析
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機站點 | 投訴建議
工業(yè)和信息化部備案號:滇ICP備2023014141號-1 云南省教育廳備案號:云教ICP備0901021 滇公網(wǎng)安備53010202001879號 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號:hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報警專用圖標(biāo)