Detailed explanation of the principle examples of chain operation in PHP

墨辰丷
Release: 2023-03-28 22:48:02
Original
1585 people have browsed it

This article mainly introduces the detailed examples of the principles of chain operation in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

There are multiple methods in a class. When you instantiate this class and call the method, you can only call it 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
}
}
Copy after login

index.php

<?php

$db = new db();

$db->where();
$db->order();
$db->limit();
Copy after login

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;
}
}
Copy after login

index.php

<?php

$db = new db();

$db->where()->order()->limit();
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

phpExample of how to click to download a file on the current page

phpImplement database operation Model class

##PHP foreach implements traversal of multi-dimensional arrays

The above is the detailed content of Detailed explanation of the principle examples of chain operation in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!