$id]. * * @throws \Exception On error. */ public function __construct(string $table, ?array $filters=null) { if (empty($table) === true) { throw new \Exception( get_class($this).' error, table name is not defined' ); } $this->table = $table; if (is_array($filters) === true) { // New one. $data = \db_get_row_filter($this->table, $filters); if ($data === false) { throw new \Exception( get_class($this).' error, entity not found' ); } // Map fields. foreach ($data as $k => $v) { $this->fields[$k] = $v; } } else { // Empty one. $data = \db_get_all_rows_sql( sprintf( 'SHOW COLUMNS FROM %s', $this->table ) ); foreach ($data as $row) { $this->fields[$row['Field']] = null; } } } /** * Dynamically call methods in this object. * * @param string $methodName Name of target method or attribute. * @param array $params Arguments for target method. * * @return mixed Return of method. * @throws \Exception On error. */ public function __call(string $methodName, ?array $params=null) { // Prioritize written methods over dynamic ones. if (method_exists($this, $methodName) === true) { return $this->{$methodName}($params); } if (array_key_exists($methodName, $this->fields) === true) { if (empty($params) === true) { return $this->fields[$methodName]; } else { $this->fields[$methodName] = $params[0]; } return null; } throw new \Exception( get_class($this).' error, method '.$methodName.' does not exist' ); } /** * Returns current object as array. * * @return array Of fields. */ public function toArray() { return $this->fields; } /** * Saves current object definition to database. * * @return boolean Success or not. */ public abstract function save(); }