This article analyzes the underlying principles of CodeIgniter's coherent operations. Share it with everyone for your reference, the details are as follows:
PHP oop coherent operation principle
The-> symbol actually passes the object pointer. Maybe that's not the right thing to say.
However, we can understand it this way.
Not much to say. Put the code.
Common usage:
<?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(); ?>
Consistent usage:
<?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(); ?>
Did you see it?
Connected. You can string operations together.
Looks much more intuitive. It’s also much easier when reading code.
All operations in the class return a pointer.
$this.
It is equivalent to the object you initialized $oktest
So the following operations can be continued.
Try to remove
from each operationreturn $this
You will see an error message.
Example:
<?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(); ?>
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php excellent development framework summary", "ThinkPHP introductory tutorial", "Summary of Common Methods in ThinkPHP", "Introduction Tutorial on Zend FrameWork Framework", "Introduction Tutorial on PHP Object-Oriented Programming", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.