3v4l.org

run code in 300+ PHP versions simultaneously
<?php class NewClas { public $id; //here's your registry protected static $registry = array(); public function __construct($id) { $this->id = $id; $this->checkVars(); //if not failed, add to registry: self::$registry[] = $id; } public function checkVars() { if (empty($this->id)) { trigger_error('ID is a required parameter.'); } //checking if it's already used: elseif (in_array($this->id, self::$registry)) { 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');

preferences:
36.75 ms | 402 KiB | 5 Q