- trigger_error: documentation ( source)
<?php
class Invariant{
private $value;
private $check;
private $description;
function __construct($check, $description){
$this->check = $check;
$this->description = $description;
}
function set($value){
$check = $this->check;
if($check($value)){
$this->value = $value;
}else{
trigger_error("Invariant ".$this->description." violated.", E_USER_ERROR);
}
}
function get(){
return $this->value;
}
}
class A{
protected $a;
function __construct(){
$this->a = new Invariant(function($value){return $value > 100;}, '$this->a > 100');
}
}
class B extends A{
function __construct(){
parent::__construct();
$this->a->set(10); //error
$this->a->set(101); //that's ok
}
}
$b = new B();