Ais*_*ina 5 php caching multiple-instances static-variables
如果我有代码使用static变量进行缓存,如下所示:
class BossParty
{
// ...
public function getTemplate()
{
static $template;
if ($template == null)
{
$template = BossTemplate::get($this->templateID);
}
return $template;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
将$template持续跨越不同的实例BossParty?我已经尝试过检查php.net,但我能找到的只是关于静态类变量的信息.
是的,静态变量将在类的实例中持久存在.
例:
<?php
class Test {
public function __construct() {
static $foo;
if ($foo) {
echo 'found';
} else {
$foo = 'foobar';
}
}
}
$test1 = new Test();
$test2 = new Test();
// output "found"
Run Code Online (Sandbox Code Playgroud)
请注意,对于后代类也是如此.如果我们有一个Child扩展的类Test,则调用parent::__construct(无论是显式还是隐式)将使用相同的值$foo.
| 归档时间: |
|
| 查看次数: |
4630 次 |
| 最近记录: |