php - retrieving in-memory cached objects as a reference -
i'm using static method of class never instantiated create objects. objects differentiated id value. i'm trying cache objects subsequent requests of same id return reference created object.
demonstration code below:
class abc { static $cache = array(); static function get( $id ) { if ( isset( static::$cache[$id] ) ) { return static::$cache($id); } static::$cache[$id] = new abc(); return static::$cache[$id]; } } $ids = array(1,1,1,2); foreach( $ids $id ) { abc::get($id); }
this not work. when change value of 1 object independent of other objects same id. tried placing '&' in front of function name , before method call no avail.
two questions: 1. how can this. 2. necessary or there better way?
thanks.
Comments
Post a Comment