- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
public function __get($name){
if(isset($this->fields[$name])){
if(class_exists($this->fields[$name]) &&
strcasecmp($this->fields[$name], $this->vals[$name]['mdl']) == 0 &&
strcasecmp(get_parent_class($this->fields[$name]), __CLASS__) == 0){
$class = $this->fields[$name];
$id = (string)$this->vals[$name]['id'];
if(!$this->refsCache[$id])
$this->refsCache[$id] = $class::findById($this->vals[$name]['id']);
return $this->refsCache[$id];
}else
return $this->vals[$name];
}elseif(isset($this->fieldsRev[$name]))
return $this->vals[$name];
else throw new Exception('Not found field "'.$name.'" when reading in model');
}
public function __set($name, $value){
if(isset($this->fields[$name])){
if(class_exists($this->fields[$name]) &&
strcasecmp($this->fields[$name], get_class($value)) == 0 &&
strcasecmp(get_parent_class($this->fields[$name]), __CLASS__) == 0)
$this->vals[$name] = array('id' => $value->getId(), 'mdl' => strtolower($this->fields[$name]));
else
$this->vals[$name] = $value;
}elseif(isset($this->fieldsRev[$name]))
$this->vals[$name] = $value;
else throw new Exception('Not found field "'.$name.'" when writing in model');
}
Пример реализации через жопу полей для базового класс модели, с ужасным оформлением кода.