PHP object-oriented ID ing and php object-oriented ing
/* The ID ing Class is added on the basis of the data er. The main function is to save the created objects. You can directly retrieve the objects as needed, rather than duplicate creation, leading to a reduction in system performance. Some methods for calling the ID ing Class are added Based on the Data er. The sample code is as follows: */namespace woo \ domain; // ID ing class ObjectWatcher {private $ all = array (); // private static $ instance for storing objects; // private function _ construct () {} static function instance () {if (! Self: $ instance) {self: $ instance = new ObjectWatcher ();} return self: $ instance;} // obtain a unique identifier, the domain class name + ID method is used to create a unique ID. This prevents duplicate IDs when multiple database tables call this class. function globalKey (DomainObject $ obj) {$ key = get_class ($ obj ). ". ". $ obj-> getId (); return $ key;} // add object static function add (DomainObject $ obj) {$ inst = self: instance (); $ inst-> all [$ inst-> globalKey ($ obj)] = $ obj;} // obtain the object static function exists ($ classn Ame, $ id) {$ inst = self: instance (); $ key = "$ classname. $ id "; if (isset ($ inst-> all [$ key]) {return $ inst-> all [$ key];} return null ;}} namespace woo \ mapper; abstract class Mapper {// abstract base class abstract static $ PDO; // operate the pdo object function _ construct () {if (! Isset (self ::$ PDO) {$ dsn =\woo \ base \ ApplicationRegistry: getDSN (); if (is_null ($ dsn )) {throw new \ woo \ base \ AppException ("no dns");} self ::$ PDO = new \ PDO ($ dsn); self :: $ PDO-> setAttribute (\ PDO: ATTR_ERRMODE, \ PDO: ERRMODE_EXCEPTION) ;}// the newly added Method Based on the Data er is referred to as "add, the purpose here is to get the object instead of querying the database and re-create the object // (compare the code of the original data) private function getFroMap ($ id) {return \ woo \ domain \ ObjectWatcher: exists ($ this-> targetClass (), $ Id) ;}// added. The function here is to save the created object as private function addToMap (\ woo \ domain \ DomainObject $ obj) {// return \ woo \ domain \ ObjectWatcher: add ($ obj);} // compare the code of the original data er, it is found that it is not directly creating an object, but first finding it in the ID ing class. The // subclass method called only after it is not found is created and inserted into the ID ing Class, the following find method also follows this principle: function createObject ($ array) {$ old = $ this-> getFromMap ($ array ['id']); // Add if ($ old) {return $ old} // Add $ obj = $ this-> doCreateObject ($ array ); // implement $ this-> addToMap ($ obj ); // Add return $ obj;} // function find ($ id) {// obtain a piece of data from the database by ID and create it as the object $ old = $ this-> getFromMap ($ id); // Add if ($ old) {return $ old} // Add $ this-> selectStmt ()-> execute (array ($ id); $ array = $ this-> selectStmt () -> fetch (); $ this-> selectStmt ()-> closeCursor (); if (! Is_array ($ array) {return null;} if (! Isset ($ array ['id']) {return null;} $ object = $ this-> createObject ($ array); $ this-> addToMap ($ object ); // Add return $ object;} function insert (\ woo \ domain \ DomainObject $ obj) {// insert object data into the database $ this-> doInsert ($ obj ); $ this-> addToMap ($ obj); // Add} // The abstract methods to be implemented in the subclass abstract function targetClass (); ///// abstract function update (\ woo \ domain \ DomainObject $ objet); protected abstract function doCreateObject (array $ array); protected abstract function selectStmt (); protected abstract function doInsert (\ woo \ domain \ DomainObject $ object );} class SpaceMapper extends Mapper {// other Code has already been implemented in the er article. Here, I skipped the implementation //............. // class name, which generates the protected function targetClass () {return "woo \ domain \ Space" ;}} for unique identifiers in the ID ing Class ";}}