The following PHP code example, the function is to help the user to reset the password, Requestresetpassword is to receive the user to reset the password request and do a corresponding check. For better reusability, I assign the reset password operation separately to a new ResetPassword function, and then call SendEmail to send a notification message to the user after the password is changed.
Copy CodeThe code is as follows:
/**
* User request to reset password receiver
*/
function Requestresetpassword () {
Check if the user exists
if (!checkuserexists ($_get[' userid ')) {
Exit (' Sorry, the user does not exist, please confirm the user account. ');
}
ResetPassword ($_get[' userid ');
Finally send a message to the user
SendEmail ($_get[' userid '), ' Reset password succeeded ', ' new password is xxxx ');
Exit (' The new password has been sent to your mailbox. ');
}
/**
* Help users reset their passwords
*/
function ResetPassword ($userid) {
Check if the user exists
if (!checkuserexists ($userid)) {
return false;
}
To reset the user's password
Slightly...
return true;
}
/**
* Send an email to the user
*/
function SendEmail ($userid, $title, $content) {
Check if the user exists
if (!checkuserexists ($userid)) {
return false;
}
Send mail action
Slightly...
return true;
}
/**
* Check if a user exists
*/
function Checkuserexists ($userid) {
$user = GetUserInfo ($userid);
Return!empty ($user);
}
/**
* Get data from a user
*/
function GetUserInfo ($userid) {
Suppose I have a query function that is used to query the database and return the data
$user = Query ("SELECT * from ' user ' WHERE ' uid ' =". Intval ($userid));
Return Is_array ($user)? $user: Array ();
}
Now the problem is that all three of these functions use the Checkuserexists function to check that the user does not exist and that the database is queried three times, which brings some extra overhead.
If you want to remove any one of the checkuserexists between the three, it seems possible. However, if there are certain features to call ResetPassword or SendEmail after the user does not exist, the system may get an error.
Another solution is to write the logic of the ResetPassword into the Requestresetpassword, and then a little more, and write the sendemail logic in it. This reduces the function call, and the database query becomes one time, and the performance is improved. But the ability to reset passwords and send messages will not be reused, and it violates the principle of single responsibility and increases the complexity of the code.
However, because the function separation and reusability are good, if the actual performance is affected, may consider the cache method to reduce the database query, I changed their common checkuserexists function:
Copy CodeThe code is as follows:
/**
* Check if a user exists
*/
function Checkuserexists ($userid) {
Add a cache to record the results of checking users
Static $cache = Array ();
Check if the current user has checked once
if (Isset ($cache [$userid])) {
return $cache [$userid];
}
$user = GetUserInfo ($userid);
Record the results in the cache
$cache [$userid] =!empty ($user);
return $cache [$userid];
}
You can also change the GetUserInfo function in the same way.
As you can see here, when the reusability of code increases, it is very easy to improve performance, and the bottleneck of performance is easy to be discovered and modified.
Although the performance impact of this example is not large enough, there are some more impact, such as traversal, I may be used for reuse to wrap the traversal into a function, and use it more than once. These costs have had too much impact, or minimal, on my project at all. So I prefer to spend more time on how to improve the reusability and maintainability of the code, rather than the waste of this performance. If the actual performance is not up to the requirement, you can also weigh the additional hardware configuration.
http://www.bkjia.com/PHPjc/825179.html www.bkjia.com true http://www.bkjia.com/PHPjc/825179.html techarticle The following PHP code example, the function is to help the user to reset the password, Requestresetpassword is to receive the user to reset the password request and do a corresponding check. For better reusability, I ...