Home >Backend Development >PHP Tutorial >Use static to avoid 'repeated reading'_PHP tutorial

Use static to avoid 'repeated reading'_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 14:55:41953browse

In the development of more complex web programs, due to the object-oriented data operation method or the business logic is too complex, developers often unconsciously read data repeatedly during the development process.
For example:
$result1 = tableobjectPeer::getResult($var1,$var2,$var3);
When developers need the table data, they often call the tableobjectPeer::getResult method directly.
Or when the program is forwarding, it will also cause repeated calls to the tableobjectPeer::getResult method, resulting in "repeated reading".

To avoid similar "repeated reading", the most important way is for developers to have the awareness of "avoiding repeated reading" when developing code.
In fact, just do it where needed after:
$result1 = tableobjectPeer::getResult($var1,$var2,$var3);

$result2 = $result1;
$ result3 = $result1;
is enough. In this way, a large number of "repeated reads" can be avoided.
But if the developers didn't do this at the beginning, it may be a lot of work to refactor in this area.
In addition, due to the forward() in the framework, it is easy to cause "repeated reading". If "repeated reading" is caused by forward(), then this method is not feasible (this may be related to different development languages ​​and different development frameworks, as is the case in PHP's symfony framework).

Therefore, while using the above method for optimization, for some more complex situations, we decided to use another method: Use static and set the variable to a static variable to avoid repeated reading of data. .

The following is the quoted content:
以下为引用的内容:
//要改写的函数也请加上$is_static=1变量,用来控制是否开启 static。
function staticFunc ($var1,$var2,$var3,$is_static=1)
{
if ( $is_static == 1 )//默认需要缓存函数结果
{
static $result_array;//该数组用来保存函数的结果,支持不同参数的结果缓存
$vars_string = serialize( func_get_args() );

if ( empty( $result_array ) )//第一次运行需要初始化
{
$result_array = array();
}

if ( array_key_exists( $vars_string, $result_array ) )//参数已经存在
{
return $result_array[$vars_string];//返回静态变量中已经保存的结果
}else//参数不存在
{
$result_array[$vars_string] = '';//后面会把结果补充进来
}
}else//不利用static 缓冲结果
{
if ( empty( $result_array ) )
{
$result_array = array();
}
}

$result_array[$vars_string] = rand();//获取结果,请把获取的代码放在此处即可
return $result_array[$vars_string];
}

echo staticFunc(1,2,3);
echo "
";
echo staticFunc(2,2,2);
echo "
";
echo staticFunc(1,2,3);
echo "
";
echo staticFunc(2,2,2);
echo "
";
echo staticFunc(3,3,3);
echo "
";
echo staticFunc(3,3,3,0);
echo "
";
?>
"; echo staticFunc(2,2,2); echo "
"; echo staticFunc(1,2,3); echo "
"; echo staticFunc(2,2,2); echo "< ;br>"; echo staticFunc(3,3,3); echo "
"; echo staticFunc(3,3,3,0); echo "< ;br>"; ?>

Running the above code produces results similar to:
16667
8888
16667
8888
2193
1014
It can be seen that the results of lines 1 and 3 are consistent, and the results of lines 2 and 4 are consistent, indicating that as long as the parameters of the function are the same, the function results are effectively "cache".
From lines 4 and 5, it can be seen that setting the $is_static variable can effectively control whether to enable "cache".

Supplement: Using the static method above 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 just the "cache" of the function result within the thread, its principle is similar to other methods of cache, that is, the cache key must be constructed for different parameters (different situations).

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364345.htmlTechArticleIn the development of more complex web programs, due to the use of object-oriented data operation methods, or the business logic is too complex , developers often read repeatedly unconsciously during the development process...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn