My question is simple, but I can't seem to find any answer for it online. I will probably jump straight into the code:
class NewClas {
public $id;
public function __construct($id) {
$this->id = $id;
$this->checkVars();
}
public function checkVars() {
if (empty($this->id)) {
trigger_error('ID is a required parameter.');
} elseif ($this->id WAS USED IN A PREVIOUS OBJECT) {
trigger_error('ID "'.$this->id.'" was used already. Please insert a unique name.');
}
}
}
$object1 = new NewClass('id1');
$object2 = new NewClass('id2');
$object3 = new NewClass('id1'); // throws error, because id1 was already used
So - is it possible to check for uniqueness of a value of the property among all instances of the class? I am just getting started with OOP, so please go easy on me. :)
Also, I am aware of spl_object_hash
but I would prefer work with IDs as readable strings, specified by a user.
Thanks in advance!