Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82221 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 36104 invoked from network); 9 Feb 2015 04:44:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Feb 2015 04:44:27 -0000 Authentication-Results: pb1.pair.com header.from=guilhermeblanco@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=guilhermeblanco@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.178 as permitted sender) X-PHP-List-Original-Sender: guilhermeblanco@gmail.com X-Host-Fingerprint: 209.85.213.178 mail-ig0-f178.google.com Received: from [209.85.213.178] ([209.85.213.178:46976] helo=mail-ig0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 23/75-26926-A2B38D45 for ; Sun, 08 Feb 2015 23:44:27 -0500 Received: by mail-ig0-f178.google.com with SMTP id hl2so14018916igb.5 for ; Sun, 08 Feb 2015 20:44:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=XUnbcWZyoulc3eXmE7Bo0DnttHYlbusYesvMj9xTPVQ=; b=Qy4NDp1kAY/hGwxbldbOa4dswBsXCsXjX9lKSspexBfEIZjkZtrHIQ9PKUWC5L+Iu4 m1z4s3poUg56lEw+1P3/LKQKYmo/siyL+HBCVtGLD950Xze6neCw4Rsi0GSzypLr4OUW sMV3d89D5LmJxeUtnFVaKdTRlBy9hV27VGyP48yHAI6ABcmDcSE0tLPNFJBHIVcTKMZm MvxdCqDAkO6Zzuhuz9CisZwJb+RyK/xTp2mU6At7tjXx+cfmwRzZSfcgT+P96GUfjXGh GdFPTqxgRiznYBSlFyvFiJlhKh8HAJqxzH0emCyNElaaNzj/sv3Utjukw0GaBuitDoaT UUng== X-Received: by 10.50.79.163 with SMTP id k3mr14827775igx.30.1423457064441; Sun, 08 Feb 2015 20:44:24 -0800 (PST) MIME-Version: 1.0 Received: by 10.64.238.75 with HTTP; Sun, 8 Feb 2015 20:44:03 -0800 (PST) In-Reply-To: References: <54D7ED22.3080001@gmail.com> Date: Sun, 8 Feb 2015 23:44:03 -0500 Message-ID: To: Yasuo Ohgaki Cc: Joe Watkins , Stanislav Malyshev , Dmitry Stogov , PHP Internals Content-Type: multipart/alternative; boundary=089e01184038addbf3050ea06cf8 Subject: Re: [PHP-DEV] Design by Contract From: guilhermeblanco@gmail.com ("guilhermeblanco@gmail.com") --089e01184038addbf3050ea06cf8 Content-Type: text/plain; charset=UTF-8 Hi Yasuo, 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 []s, On Sun, Feb 8, 2015 at 11:24 PM, Yasuo Ohgaki wrote: > Hi Guilherme, > > On Mon, Feb 9, 2015 at 11:58 AM, guilhermeblanco@gmail.com < > guilhermeblanco@gmail.com> wrote: > >> Design by Contract could be used at the top of Annotation if (and only >> if) it also had support for Interceptors. Since it could potentially be a >> nightmare for PHP, I don't think it's time to propose something at the top >> of another thing that is still far from being reality. >> It would follow somehow a similar approach of what Symfony validators do: >> http://symfony.com/doc/current/book/validation.html but it would be >> called at possible 3 times: pre-execution, in-execution (using around and >> bound to an external class) and post-execution. >> However, I keep myself asking if this is a good idea. It obviously brings >> a certain level of AOP to PHP, but I'd rather take a simplistic approach >> such as the one suggested by Stas but with a few adjustments. >> His approach is more inline to C and C++, but I like a more customizable >> Java style assertion support >> http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html >> Example: >> >> function foo($name) { >> assert $name !== '': "Name must not be an empty value"; >> } >> >> This would be similar to this: >> >> function foo($name) { >> if ($name === null) { >> throw new AssertionError("Name must not be an empty value"); >> } >> } >> >> Basically, new grammar to be supported would be something like this: >> >> assert_statement: >> T_ASSERT expr ':' expr ';' >> ; >> >> Where the first expr is a boolean expression and the second expr must >> return a value (cannot be null). >> >> This would be a good start for PHP, even though Java does not recommend >> to use this for argument's method check ( >> http://docs.oracle.com/javase/8/docs/technotes/guides/language/assert.html#usage), >> but I consider that since PHP is not a strict language, we do not have the >> same privileges by automating this check form arguments itself. >> >> That is my personal suggestion for DbC, which does not fully align with >> what it was proposed, but since you asked... here it is. >> > > Thank you. > Since assert is going to have zero performance penalty, Stas proposal > would work. > We have to think of how invariants should be. > > Joe, could you add "Proposal" section to the RFC. > https://wiki.php.net/rfc/expectations > Currently, it does not have "Proposal" section and it's harder to > understand what the > RFC is. Then, could you start vote? New assert is valuable regardless of > DbC. > > The RFC will encourage users to use assert(), my little concern with > Stas's proposal > is misuse/abuse of assert(). Stas's proposal is simplest, but I'm not sure > if we should go > for it or not. > > Regards, > > -- > Yasuo Ohgaki > yohgaki@ohgaki.net > -- Guilherme Blanco MSN: guilhermeblanco@hotmail.com GTalk: guilhermeblanco Toronto - ON/Canada --089e01184038addbf3050ea06cf8--