Underlying Principle Analysis of CodeIgniter coherent operations _ php instance

Source: Internet
Author: User
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();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.