- in_array: documentation ( source)
- trigger_error: documentation ( source)
<?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');