Re: Fwd: [PHP-DEV] [RFC] PHP Attributes
| From: | Fleshgrinder | Date: | Thu, 28 Apr 2016 19:42:50 +0000 |
| Subject: | Re: Fwd: [PHP-DEV] [RFC] PHP Attributes | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92900@lists.php.net to get a copy of this message | ||
Is there a reason why you think that Design by Contract (DbC) should be
implemented via annotations/attributes?
I personally think that such a system should be implemented directly in
the language, like Eiffel has it. I even think that it would be easy to
add it without any BC. It might be a bit more complicated to implement
thought.
[assert]
assert.active = 1
assert.invariant = 1
assert.require = 1 ; preconditions
assert.ensure = 1 ; postconditions
class PreconditionError extends AssertionError {}
class PostconditionError extends AssertionError {}
function hello(string $who): string {
return "Hello {$who}\n";
}
require {
# argument must have content
$who !== '';
# argument cannot contain unprintable characters
ctype_print($who);
}
hello('');
// PHP Warning: require(): argument must have content: "$who !== ''"
// failed in ...
hello("\0");
// PHP Warning: require(): argument cannot contain unprintable
// characters: "ctype_print($who)" failed in ...
class A {
private DateTimeImmutable $created;
private DateTimeImmutable $changed;
function f() {}
require {}
// Special scope "old" available...
ensure {
# error message
$this->x = old::$x - 42;
}
}
ensure {
# created cannot be less than changed time
$this->created <= $this->changed;
}
--
Richard "Fleshgrinder" Fussenegger
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc
Attachment: [application/pgp-signature] OpenPGP digital signature signature.asc