Re: Design by Contract

From: Date: Mon, 09 Feb 2015 05:52:19 +0000
Subject: Re: Design by Contract
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-82225@lists.php.net to get a copy of this message
Hi Guilherme, On Mon, Feb 9, 2015 at 1:44 PM, guilhermeblanco@gmail.com < guilhermeblanco@gmail.com> wrote: > Class invariants could be done as this example: > > class Foo { > private function isValidState() { > // Checks class's state and returns boolean > } > > public function doSomething($args) { > // Some complex operation over Foo instance using $args > // then... > > assert $this->isValidState(): $this; // it would call > Foo::__toString() for AssertionError message > } > } > > Another example of pre and post conditions: > > function foo($number) { > // pre-condition > assert is_numeric($number): '...'; > > // Some expensive operation creating $value as response > > // post-condition > assert someCheckWith($value): '...'; > return $value; > } > > A good explanation about use-cases can be found here > > http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#class-invariants > Thank you for the info. I had this syntax in mind. class Foo { __invariant() { // Ignored for production. // Check class state, every method calls invoke this method before/after except // __construct()/__wakeup()/__set_state() - after only // __destruct() - before only } } // Framework developer shouldn't use this. Leave this function for app developers. function __invariant() { // Ignored for production. // Invariant conditions of functions invoked for every function calls // to check app state. e.g. global vars,etc } __invariant() has similar issue as __autoload(), we may be better to provide generic registration method. Just my initial thought for discussion. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net

« previous php.internals (#82225) next »