Is it necessary to cache objects?
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;
}
}
}
精彩评论