Domain modeling base class
Release: 2016-07-25 08:48:16
Original
1068 people have browsed it
Implemented lazy loading and registration as the only one Java needs to introduce the concepts of factories and warehouses, while PHP only needs two or three lines of code to complete the same function
- /**
- * Domain model parent class
- * Implement delayed data loading and registration as unique
- * @author liuxu
- *
- */
- abstract class Domain
- {
-
- protected $_id;
- protected $_id2;
-
- protected $_fields = array();
-
- protected $_loadedById = false;
-
- static protected $_objs;
-
- private function __construct($id,$id2=0)
- {
- $this->_id = $id?$id:0;
- $this->_id2 = $id2?$id2: 0;
- }
-
- static public function factory($id,$id2=0)
- {
- $class = get_called_class();
- if(!isset(self::$_objs[$class][$id][$ id2]))//Register as unique
- {
- self::$_objs[$class][$id][$id2] = new $class($id,$id2);
- }
-
- return self::$ _objs[$class][$id][$id2];
- }
-
- public function load($fields=array())
- {
- if($fields)
- {
- foreach($fields as $key=> $value)
- {
- $this->_fields[$key] = $value;
- }
- }
- }
-
- public function wash()
- {
- $this->_fields = array();
- }
-
- //Load complete data
- abstract protected function loadById();
-
- public function __get($field)
- {
- if(!isset($this->_fields[$field]) && !$this-> _loadedById)
- {
- //Lazy loading
- $this->loadById();
- $this->_loadedById = true;
- }
-
- $value = isset($this->_fields[$field]) ?$this->_fields[$field]:null;
- return $value;
- }
-
-
-
- }
Copy code
|
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31