Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82242 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 77591 invoked from network); 9 Feb 2015 08:40:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Feb 2015 08:40:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=dmitry@zend.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=dmitry@zend.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain zend.com designates 209.85.220.172 as permitted sender) X-PHP-List-Original-Sender: dmitry@zend.com X-Host-Fingerprint: 209.85.220.172 mail-vc0-f172.google.com Received: from [209.85.220.172] ([209.85.220.172:43404] helo=mail-vc0-f172.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6F/35-50460-17278D45 for ; Mon, 09 Feb 2015 03:40:18 -0500 Received: by mail-vc0-f172.google.com with SMTP id le20so9219990vcb.3 for ; Mon, 09 Feb 2015 00:40:15 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=PESO+T91cJ0qGLoWhaNrDxetu9a5X6E3qQnMF9RWhuM=; b=C0QkP5dqUFl7bUujQcLGxNWUdkNhZmDAKmhy0L7F3U3dRlSglTzXyEeEpSxdKSh3nZ omecjKxqWuaHghx4WLBg9pT2lofnm7T/K1Y7p+j+kbgvNq+/jLfYDfD9WEr9t/4rpV6R HJqRxk0DZDnb0xA6sD9J3dZN1wXvQGQxEEOGPy5Yz0WMVKIHY5KnWocfEMxVj9aKFpQ4 TIQWe2VsOi/vBQCqIZRzMuzmOkaQlAWWVxWQO6cUyzO7WPMFzn1TbIpMVN10/AqAk44g B2O6hcRy+WV+sx9zixLRcpdPotIkdaywqglbMPHeF8IU9yedpZ04lW5Jq+nmAlONRXBj z9uw== X-Gm-Message-State: ALoCoQmqhxA1gTZw+j1VssEpYag+GGwgawxTR5c7v1iZ4uVXHAy8kyqa34TFqTeL/QgGuHYzlBIJ6dITcwgbsgKCbU6Lzo3xDGdDwqvyaOF/MP8+jGzYN2vzyJbaQanA5pbGy29MxPryRjpiBRLycNgOWv8OAZFrqA== MIME-Version: 1.0 X-Received: by 10.52.26.110 with SMTP id k14mr7971895vdg.65.1423471215749; Mon, 09 Feb 2015 00:40:15 -0800 (PST) Received: by 10.52.74.73 with HTTP; Mon, 9 Feb 2015 00:40:15 -0800 (PST) In-Reply-To: References: <54D7ED22.3080001@gmail.com> Date: Mon, 9 Feb 2015 12:40:15 +0400 Message-ID: To: Yasuo Ohgaki , Alexander Lisachenko , "guilhermeblanco@gmail.com" Cc: Joe Watkins , Stanislav Malyshev , PHP Internals Content-Type: multipart/alternative; boundary=20cf307d048229c4a4050ea3b8b4 Subject: Re: [PHP-DEV] Design by Contract From: dmitry@zend.com (Dmitry Stogov) --20cf307d048229c4a4050ea3b8b4 Content-Type: text/plain; charset=UTF-8 Hi, I'll try to answer to all the emails at once. Personally, I like the D approach more, but doc-comment proposal has its advantages as well. The problem that doc-comment solution is external to PHP core. In my opinion, it should be implemented as a separate preprocessor that reads doc-comments and generates PHP code with asserts(). I don't see how this may be implemented in PHP core itself, and I most probably won't be evolved in development of other project. Something similar was already implemented... Alexander, could you please send link to implentation/dicumentation and explain how it works (in few words). Yasuo, I think you should describe the D proposal as well. I'm not sure if it should be done in the same RFC (it's already big) or a new one. Any solution would require zero-cost assert (or similar concept) implemented in php core. Thanks. Dmitry. On Mon, Feb 9, 2015 at 8:52 AM, Yasuo Ohgaki wrote: > 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 > --20cf307d048229c4a4050ea3b8b4--