English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Principle of Chain Operation Implementation in PHP

In a class, if there are multiple methods, when you instantiate this class and call the method, you can only call them one by one, similar to:

db.php

<?php
class db
{
public function where()
{
//code here
}
public function order()
{
//code here
}
public function limit()
{
//code here
}
} 

index.php

<?php
$db = new db();
$db->where();
$db->order();
$db->limit(); 

If you want to implement chain calls, you need to add return $this at the end of the method.

db.php

<?php
class db
{
public function where()
{
//code here
return $this;
}
public function order()
{
//code here
return $this;
}
public function limit()
{
//code here
return $this;
}
} 

index.php

<?php
$db = new db();
$db->where();->order();->limit();

This article about the principle of chain operation in PHP is all the content I share with you. I hope it can be a reference for you, and I also hope that everyone will support the yells tutorial.

You May Also Like