You can use magic setters / getters:
class event{ protected static $table_name='tName'; protected static $db_fields = array('field1','field2','field3','field4','field5'); public function __get($key) { if(!in_array($key, static::$db_fields)) throw new Exception( $key . " doesn't exist."); return $this -> $key; } public function __set($key, $value) { if(!in_array($key, static::$db_fields)) throw new Exception( $key . " doesn't exist."); $this -> $key = $value; } }
This way you will not hit values ββoutside of your list:
$event -> field1 = 'hello'; // --> OK $event -> field17 = 'hello'; // --> Exception: field17 doesn't exist echo $event -> field1; // --> OK echo $event -> field17; // --> Exception: field17 doesn't exist
As for having an explicit declaration of a public variable in your code, you do not need to if you do not need to iterate over your objects, but in this case you would execute an Iterator based on your static field.
source share