Re: DbC for PHP?

From: Date: Wed, 29 Jan 2014 04:18:05 +0000
Subject: Re: DbC for PHP?
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-71712@lists.php.net to get a copy of this message
Hi Sebastian, On Tue, Jan 28, 2014 at 5:36 PM, Sebastian Krebs <krebs.seb@gmail.com>wrote: > I'm wandering if we could have DbC (Design by Contract) for PHP 6. >> http://en.wikipedia.org/wiki/Design_by_contract >> >> There is user land implementation already. >> >> >> https://gitorious.org/higher-order-php/php-contracts/source/5b0a53b9732f0e4dbe79345212c84c74526def3b >> : >> >> It could be done with dbc mode INI switch as follows. >> >> When dbc=on, automatically includes >> >> include __dbc_ . __SCRIPT_NAME__; >> >> when a script is loaded. No error if there is not the script. >> When function/method is called >> >> __dbc_pre_. __FUNCTION__() is called before calling function(). >> __dbc_post_.___FUNCTION__() is called after function() call. >> >> No error if there is not the function. >> > > Couldn't you achieve the same with assert()? > Assert can be used for DbC and there are such implementations. Precondition is easy with assert(), but post condition is rather difficult since we need assert() before every 'return'. This could damage production code (code executed on production environment) and contract readability. It increases chances of omitting post condition check by mistake. Similar mistake may happen with this. We may better to have option to warn non existent pre/post check. Like Alexander explained, pure OO code can use DbC. It may be enough for people who are interested in DbC. This is just an idea. It's simple, powerful and easy to adopt. Since there is no DbC code in production code, it's fast while assert() need compilation overhead. It does not matter much with opcache, though. With this approach, user has to look up contract in other file. This is disadvantage over other methods. I wander how many of us are interested in such feature. Regards, -- Yasuo Ohgaki yohgaki@ohgaki.net

« previous php.internals (#71712) next »