You can check php dynamic property constraint . The idea is to indicate which properties exist and can be changed in the array, and then check if the updated property (as in Alix's answer, the example expanded below) is part of this permission list.
class User { public $username; public $password; private $non_dynamic_props = array('username','password'); public function __set($key, $value) { if($this->check_legal_assignment($key)) $this->$$key = $value; else throw new Exception('illegal');
source share