Preface
My life’s articles will be put here, my blog, I hope every line of code, every paragraph of text can help you.https://github.com/CrazyCodes …
Hello everyone, I’m CrazyCodes. today we won’t talk about tools, specifications, etc. we’ll talk about how to write a “good” code. this article takes the code I encountered in my career as an example. if there is any discrepancy, please raise objection in the comment area. thank you.
Search function
Search is very common, complex search mostly travel in the background, for example chestnut, probably the demand is like this
This is a background user list search function
Search criteria | Can we go in parallel? | Is it required |
---|---|---|
User name | Sure | no |
Mobile phone number | Sure | no |
Is it certified | Sure | Yes |
User gender | Sure | no |
Last login time | Sure | no |
Account balance | Sure | no |
Beginner code
Seeing these examples, did you have to start building wheels again?
Take the original example as an example, at first you may write like this (the following is pseudo code)
if (IS_POST) {
$like = '';
if (isset($_POST['username'])) {
$username = $_POST['username'];
$like .= "username like '%" . $username . "%' and ";
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
$like .= "phone like '%" . $phone . "%' and";
}
if ($_POST['is_auth']) {
$isAuth = $_POST['is_auth'];
$like .= "is_auth like '%" . $isAuth . "%' and";
}
if ($_POST['sex']) {
$sex = $_POST['sex'];
$like .= "sex like '%" . $sex . "%' and";
}
if ($_POST['time']) {
$time = $_POST['time'];
$like .= "time like '%" . $time . "%' and";
}
if ($_POST['wallet']) {
$wallet = $_POST['wallet'];
$like .= "wallet like '%" . $wallet . "%' and";
}
$like = rtrim($like, 'and');
$sql = "SELECT * FROM `user` WHERE {$like}";
} else {
return view('user');
}
Encapsulation
Well … it’s not bad, the structure is clear, the traditional beginner’s bar code, let’s encapsulate a few pieces of code first.
function post($param)
{
return isset($_POST[$param]) ? $_POST[$param] : null;
}
if (IS_POST) {
$like = '';
if (post('username')) {
$username = post('username');
$like .= "username like '%" . $username . "%' and ";
}
if (post('phone')) {
$phone = post('phone');
$like .= "phone like '%" . $phone . "%' and";
}
if (post('is_auth')) {
$isAuth = post('is_auth');
$like .= "is_auth like '%" . $isAuth . "%' and";
}
if (post('sex')) {
$sex = post('sex');
$like .= "sex like '%" . $sex . "%' and";
}
if (post('time')) {
$time = post('time');
$like .= "time like '%" . $time . "%' and";
}
if (post('wallet')) {
$wallet = post('wallet');
$like .= "wallet like '%" . $wallet . "%' and";
}
$like = rtrim($like, 'and');
$sql = "SELECT * FROM `user` WHERE {$like}";
} else {
return view('user');
}
Use iteration appropriately
Well, at least we can freely control the post method, but the maintainability of this kind of procedural code is too low, so we can improve it.
function post($param)
{
return isset($_POST[$param]) ? $_POST[$param] : false;
}
function postAll()
{
return $_POST;
}
if (IS_POST) {
$like = '';
foreach (postAll() as $key => $value) {
if (post($key)) {
$like .= "{$key} like '%{$value}%' and";
}
}
$like = rtrim($like, 'and');
$sql = "SELECT * FROM `user` WHERE {$like}";
} else {
return view('user');
}
Object oriented
Adding iterative code seems a bit neat. As a PHP programmer, writing code is not object-oriented and unreliable. Add class
function request($param = null)
{
return new Request($param);
}
class Request
{
public function __construct(string $param = null)
{
return isset($_POST[$param]) ? $_POST[$param] : false;
}
public function all()
{
return $_POST;
}
}
class User
{
public function index()
{
if (IS_POST) {
$like = '';
foreach (request()->all() as $key => $value) {
if (request($key)) {
$like .= "{$key} like '%{$value}%' and";
}
}
$like = rtrim($like, 'and');
$sql = "SELECT * FROM `user` WHERE {$like}";
} else {
return view('user');
}
}
}
Transformation of User
We are reforming User’s class, making some judgments and screening.
function request($param = null)
{
return new Request($param);
}
class Request
{
public function __construct(string $param = null)
{
return isset($_POST[$param]) ? $_POST[$param] : false;
}
public function all()
{
return $_POST;
}
}
class User
{
public $request = [
'username',
'phone',
'is_auth',
'sex',
'time',
'wallet'
];
public function index()
{
if (IS_POST) {
$like = '';
foreach (request()->all() as $key => $value) {
if (in_array($key, $this->request) && request($key)) {
$like .= sprintf("%s like %s and", $key, $value);
}
}
$like = rtrim($like, 'and');
$sql = "SELECT * FROM `user` WHERE {$like}";
} else {
return view('user');
}
}
}
This is about the same, the comparison of real codes may still be far from each other. The purpose of my writing this article is not to teach you how to write codes, but to explain that codes are not one-off and should be modified many times to make codes maintainable, extensible, etc. and various “natures”
Thank you
Thank you for seeing here. I hope this article can help you. Thank you