Use static to avoid the "repeat read" _php tutorial

Source: Internet
Author: User
Tags vars
In the more complex web program development, due to the use of object-oriented data manipulation, or business logic is too complex, developers in the development process, often unconsciously read data repeatedly.
Like what:
$result 1 = tableobjectpeer::getresult ($var 1, $var 2, $var 3);
Developers often call the Tableobjectpeer::getresult method directly when they need the table data.
Or, when the program is forward, it also causes repeated calls to the Tableobjectpeer::getresult method, resulting in "repeated reads."

The most important way to avoid a similar "repeat reading" is for developers to have the "avoid repetition of reading" Awareness when developing code.
In fact, as long as:
$result 1 = tableobjectpeer::getresult ($var 1, $var 2, $var 3);
After that, the place to be
$result 2 = $result 1;
$result 3 = $result 1;
Can. This way, you can avoid a lot of "repeat reading".
But if the open staff did not do so in the first place, the refactoring could be a lot of work.
In addition, the forward () in the framework is prone to "repetitive reading". If "Repeat read" is caused by forward (), then the method is not possible (this may be related to different development languages, different development frameworks, as in PHP's Symfony framework).

Therefore, while the above method is optimized, for some more complex situations, it is decided to take another approach: use static to set variables as static variables to avoid repeated reading of the data

The following is the referenced content:
The function to be rewritten is also added with the $is_static=1 variable to control whether static is turned on.
function Staticfunc ($var 1, $var 2, $var 3, $is _static=1)
{
if ($is _static = = 1)//cache function result is required by default
{
Static $result _array;//The array is used to hold the result of the function, supporting the result cache of different parameters
$vars _string = Serialize (Func_get_args ());

if (Empty ($result _array))//initialization required for first run
{
$result _array = Array ();
}

if (array_key_exists ($vars _string, $result _array))//parameter already exists
{
return $result _array[$vars _string];//Returns the saved results from a static variable
}else//parameter does not exist
{
$result _array[$vars _string] = ";//The results will be added in the following
}
}else//does not use static buffering results
{
if (Empty ($result _array))
{
$result _array = Array ();
}
}

$result _array[$vars _string] = rand ();//Get results, put the obtained code here
return $result _array[$vars _string];
}

Echo Staticfunc (a);
echo "
";
echo Staticfunc (2,2,2);
echo "
";
Echo Staticfunc (a);
echo "
";
echo Staticfunc (2,2,2);
echo "
";
echo Staticfunc (3,3,3);
echo "
";
echo Staticfunc (3,3,3,0);
echo "
";
?>

Running the above code produces a similar result:
16667
8888
16667
8888
2193
1014
As can be seen, the results of lines 1th and 3rd are consistent, and the results of line 2nd and 4th coincide, indicating that as long as the function parameters are the same, the function result is effectively "cache".
From lines 4th and 5th, you can see that setting the $is_static variable effectively controls whether the cache is turned on.

Add: The use of the static method above, can effectively avoid in a thread, repeated reading data, but the cache exists only one thread, different threads are independent of each other. Although it is only the function result "cache" within the thread, its principle is similar to the other way of caching, which is to construct the cache key for different parameters (different cases).

Transferred from: http://www.cnblogs.com/rethink/

http://www.bkjia.com/PHPjc/364345.html www.bkjia.com true http://www.bkjia.com/PHPjc/364345.html techarticle In the more complex web program development, due to the use of object-oriented data manipulation, or business logic is too complex, developers in the development process, often unconsciously read repeatedly ...

  • 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.