This article mainly introduces the underlying principle of CodeIgniter coherent operations, and analyzes the implementation principle of common phpoop coherent operations in CodeIgniter in the form of examples. It has great versatility and is easy to understand, for more information about the principle of CodeIgniter coherent operations, see this article. We will share this with you for your reference. The details are as follows:
Php oop coherent Operation Principle
-> A symbol is actually an object pointer. Maybe this is not true.
However, we can understand this.
Not much. Put the code.
Common usage:
<?phpclass 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:
<?phpclass 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();?>
No.
Connection up. You can concatenate operations.
It looks much more intuitive. It is much easier to read the code.
All operations in the class return a pointer.
$ This.
It is equivalent to the object you initialize $ oktest
Therefore, the following operations can be consecutive.
Try to remove
return $this
You will see an error message.
Example:
<?phpclass 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();?>