Use static to avoid "repeated read" _ PHP Tutorial

Source: Internet
Author: User
Use static to avoid "repeated read ". In complex web program development, because the object-oriented data operation method or business logic is too complex, developers, it is often unconsciously read repeatedly in complex web program development. because of the use of object-oriented data operations, or the business logic is too complex, developers in the development process, data is read unconsciously.
For example:
$ Result1 = tableobjectPeer: getResult ($ var1, $ var2, $ var3 );
When developers need this table data, they often call the tableobjectPeer: getResult method directly.
Or when the program performs the forward operation, it will also repeatedly call the tableobjectPeer: getResult method, resulting in "repeated read ".

To avoid similar "repeated reads", the most important way is that developers should be aware of "avoiding repeated reads" during code development.
In fact, as long:
$ Result1 = tableobjectPeer: getResult ($ var1, $ var2, $ var3 );
And then proceed
$ Result2 = $ result1;
$ Result3 = $ result1;
You can. In this way, a large number of "repeated reads" can be avoided.
However, if the open personnel did not do this at the beginning, it may be a great deal of work to reconstruct the project.
In addition, the forward () in the framework may also cause "repeated read ". If "repeated read" is caused by forward (), this method is not feasible (this may be related to different development languages and different development frameworks, in the symfony framework of php ).

Therefore, when using the above method for optimization, for some complicated situations, it is decided to adopt another method: Use static to set variables to static variables to avoid repeated data reading..

Reference content is as follows:
// Add the $ is_static = 1 variable to the function to be rewritten to control whether static is enabled.
Function staticFunc ($ var1, $ var2, $ var3, $ is_static = 1)
{
If ($ is_static = 1) // The function result needs to be cached by default.
{
Static $ result_array; // This array is used to save the result of a function and supports caching results of different parameters.
$ Vars_string = serialize (func_get_args ());

If (empty ($ result_array) // initialization is required for the first run
{
$ Result_array = array ();
}

If (array_key_exists ($ vars_string, $ result_array) // The parameter already exists.
{
Return $ result_array [$ vars_string]; // return the saved results in the static variable.
} Else // the parameter does not exist
{
$ Result_array [$ vars_string] = ''; // The result will be added later.
}
} Else // do not use static buffer results
{
If (empty ($ result_array ))
{
$ Result_array = array ();
}
}

$ Result_array [$ vars_string] = rand (); // obtain the result. put the obtained code here.
Return $ result_array [$ vars_string];
}

Echo staticFunc (1, 2, 3 );
Echo"
";
Echo staticFunc (2, 2 );
Echo"
";
Echo staticFunc (1, 2, 3 );
Echo"
";
Echo staticFunc (2, 2 );
Echo"
";
Echo staticFunc (3, 3 );
Echo"
";
Echo staticFunc (3,3, 3,0 );
Echo"
";
?>

Running the code above produces the following results:
16667
8888
16667
8888
2193
1014
It can be seen that the results of row 1st and row 3rd are the same. The results of row 2nd and row 4th are the same, indicating that as long as the parameters of the function are the same, the function results are effectively cached ".
From rows 4th and 5th, we can see that setting the $ is_static variable can effectively control whether to enable "cache ".

Supplement: The above static method can effectively avoid reading data repeatedly in one thread, but the cache only exists in one thread, and different threads are independent of each other. Although it is only the function result "cache" in the thread, its principle is similar to that of other cache methods, that is, to construct the cache key for different parameters (in different cases.

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.