开发者

Is it necessary to cache objects?

开发者 https://www.devze.com 2023-01-18 11:09 出处:网络
For a project I have some objects, one method is to load the parent entity. So I call the method like this:

For a project I have some objects, one method is to load the parent entity.

So I call the method like this: $this->getDetails()->getEntity();

So the code of getEntity is:

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

Is it necessary to load the entity into the attribute? Because when I want to l开发者_开发技巧oad it on another place it shouldnt be calling the mapper again.

Or is it that when it's loaded, the object is already in memory and I don't need to put it into the attribute?

Thanks


use singleton pattern like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消