Using PHP static variables when caching a method _php instance

Source: Internet
Author: User
Tags php code
The following PHP code example, the function is to help users 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 to a new ResetPassword function, and then call SendEmail to send a notification message to the user after changing the password.
Copy Code code as follows:

/**
* User requests to reset the password of the 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 a user's password
Slightly...
return true;
}

/**
* Send a message 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 for a user
*/
function GetUserInfo ($userid) {
Suppose I have a function in query that queries the database and returns the data
$user = Query ("SELECT * from ' user ' WHERE ' uid ' =". Intval ($userid));
Return Is_array ($user)? $user: Array ();
}

The problem now is that all three functions use the Checkuserexists function to check that the user does not exist and that the database has been queried three times, resulting in some additional overhead.
It seems possible to remove any one of the checkuserexists between the three. However, if later there are certain features to invoke ResetPassword or SendEmail, the system may have an error if the user does not exist.
Another solution is to write the logic of ResetPassword into the Requestresetpassword, and then a little further, and write the logic of SendEmail. As a function call is reduced, the database query becomes once and the performance is improved. But the ability to reset passwords and send messages will not be reused and violate the principle of single responsibility and increase the complexity of the code.
However, because 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 Code code as follows:

/**
* Check if a user exists
*/
function Checkuserexists ($userid) {
Add a cache to record the results of a user check
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, when the reusability of the code increases, it is easy to improve performance, and performance bottlenecks are easily discovered and modified.
Although the performance impact of this example is not large enough, there are some more impact, such as traversal, I may in order to reuse to encapsulate the traversal into a function, and use it many times. These costs are too much of an impact, or minimal, on my project at all. So I'm more willing to spend time on how to improve the reusability and maintainability of the code, rather than dwell on the waste of this performance. Actual performance can be weighed against increased hardware configuration if it is not really up to the requirements.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.