Analysis of Realization Mode of php Chain Operation
- 2021-12-13 16:37:30
- OfStack
This paper describes the implementation of php chain operation with examples. Share it for your reference, as follows:
Similar
$db->where("id=1")->limit("5")->order("id desc")
The implementation of chain operation
First, let's talk about the general call of methods;
namespace Com;
class Database{
function where($where){
echo $where;
}
function order($order){
echo $order;
}
function limit($limit){
echo $limit;
}
}
Call
$db = new \Com\Database();
$db->where();
$db->limit();
Disadvantages: Implementing multiple methods requires multiple line calls;
Chain operation, when the method returns
return $this;
Chain operation can be used;
namespace Com;
class Database{
function where($where){
echo $where;
return $this;
}
function order($order){
echo $order;
return $this;
}
function limit($limit){
echo $limit;
return $this;
}
}
Using chain calls:
$db = new \Com\Database();
$db->where("id=1")->limit("5")->order("id desc");
More readers interested in PHP can check out the topics on this site: "Introduction to php Object-Oriented Programming", "Encyclopedia of PHP Array (Array) Operation Skills", "Introduction to PHP Basic Syntax", "Summary of PHP Operation and Operator Usage", "Summary of php String (string) Usage", "Introduction to php+mysql Database Operation" and "Summary of php Common Database Operation Skills"
I hope this article is helpful to everyone's PHP programming.