2010-10-04 67 views
0

对于一个项目我有一些对象,一种方法是加载父实体。所以我打电话给我这样的方法: $ this-> getDetails() - > getEntity();是否需要缓存对象?

所以getEntity的代码是:

public function getEntity() 
{ 
    if (isset($this->entity)) { 
    return $this->entity; 
    } else { 
    $mapper = new Crm_Mapper_Entity(); 
    return $this->entity = $mapper->find($this->customerId); 
    } 
} 

是有必要的实体加载到属性?因为当我想把它加载到另一个地方时,它不应该再次调用映射器。

或者当它被加载时,对象已经在内存中,我不需要将它放入属性中?

感谢

回答

0

使用Singleton模式是这样的:

class MyStatic{ 
    private static $instance; 

    private __construct(){} 

    public static getInstance(){ 

    if (!empty(MyStatic::$instance;)) { 
      return MyStatic::$instance; 
     } else { 
      $mapper = new Crm_Mapper_Entity(); 
      $thisMyStatic::$instance = $mapper->find($this->customerId); 
      return $thisMyStatic::$instance; 
     } 

    } 

} 
+0

不要使用Singleton模式。 http://misko.hevery.com/2008/08/17/singletons-are-pathological-liars/ – dynamic 2011-05-31 14:24:33