<?php namespace libs; trait DataContain { protected Array $data = []; protected Array $data_fields = []; protected function set_fields(Array $fields) { $this -> data_fields = $fields; } public function __get(String $field_name) { if(!isset($this -> data[$field_name])) { throw new Exception("Field `{$field_name}` in User object not found"); } return $this -> data[$field_name]; } public function __set(String $field_name, $field_value) { if(!in_array($this -> data_fields[$field_name])) { throw new Exception("Field `{$field_name}` not provided in User object"); } $this -> data[$field_name] = $field_value; if(property_exists($this, "changed_fields") and is_array($this -> changed_fields)) { $this -> changed_fields[] = $field_name; } } }