I discovered the Design by Contract template and how to implement it in PHP. I can not find a real example of how to do this in PHP. First question Am I doing this right? The second is why the assert call is not being made?
Static class Assertsfor reusable statements:
class Asserts
{
public static function absentOrNotNumeric($value)
{
return !isset($value) ? true : is_numeric($value);
}
}
Using:
assert_options(ASSERT_ACTIVE, true);
assert_options(ASSERT_BAIL, true);
assert_options(ASSERT_WARNING, true);
assert_options(ASSERT_CALLBACK, array('UseAsserts', 'onAssertFailure'));
class UseAsserts
{
private $value;
public function __construct($value)
{
assert('Asserts::absentOrNotNumeric($value)');
$this->value = $value;
}
public static function onAssertFailure($file, $line, $message)
{
throw new Exception($message);
}
}
$fail = new UseAsserts('Should fail.');
Only a (correct) warning is triggered:
Warning: assert () [function.assert]: The statement "Asserts :: absetOrNotNumeric ($ value)" failed.
gremo source
share