iphp
iphp copied to clipboard
Context add lazyload feature
trafficstars
基类中为了方便原则, 一般会加载所有可能用到的属性到 Context 中, 但这会导致性能问题, 所以需要 lazyload 机制. 例如, 某些属性只有在子类中被用到时, 才会真正地去查询数据库, 如果子类中不使用这些属性, 则不会发生数据库请求.
原型:
Context::lazyload($name, callable $callback_func);
示例:
class AppController extends Controller
{
function init($ctx){
parent::init($ctx);
$ctx->lazyload('account', array($this, 'ctx_lazyload'));
}
private function ctx_lazyload($name, $ctx){
if($name == 'account'){
return Account::get($ctx->user->id);
}
}
}
done