Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:82225 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 46031 invoked from network); 9 Feb 2015 05:53:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 9 Feb 2015 05:53:03 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.43 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.216.43 mail-qa0-f43.google.com Received: from [209.85.216.43] ([209.85.216.43:50703] helo=mail-qa0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/F6-26926-E3B48D45 for ; Mon, 09 Feb 2015 00:53:02 -0500 Received: by mail-qa0-f43.google.com with SMTP id bm13so5134847qab.2 for ; Sun, 08 Feb 2015 21:52:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=CQm5G1GIdLvpqXHzJbf/xQ3FQDNVjb9TA+sBcUYBdm0=; b=Ywhp4qHVPp3+0gQrAD55AgyuPLDhZIzFF7iGdVRjaFr8fkQlmjs6wOr765yZsq9Y/S BR+S7mlihwRFiEXaOJYAqQDF/Q1XNhqKA3W5cCc76+hZHyYRTzdJdRitUKr72kTNfleh FPL+qKsMXV9ligIpPDWXt74D6ssIKyCi6FAfyR+DoxtNeUlAw8wX3ZAzUQgjY+Vizsow 7v5xu8dmxcjvWtNM5AP0cE9pWbMuefIpkXX/soa5V3UcVHt3CJgbgJEijI1G476SHjBr Blot9tL7w7GesYrYhf7rodGmlRmB8NsBVytvcUhRcyCgY1CIVvo5BTGcfKW5Z0k7Sf1c Q/FQ== X-Received: by 10.224.161.138 with SMTP id r10mr37457946qax.21.1423461179372; Sun, 08 Feb 2015 21:52:59 -0800 (PST) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.229.28.72 with HTTP; Sun, 8 Feb 2015 21:52:19 -0800 (PST) In-Reply-To: References: <54D7ED22.3080001@gmail.com> Date: Mon, 9 Feb 2015 14:52:19 +0900 X-Google-Sender-Auth: 4VO6gZE6PHU-GQN4LDK2xr5LuUM Message-ID: To: "guilhermeblanco@gmail.com" Cc: Joe Watkins , Stanislav Malyshev , Dmitry Stogov , PHP Internals Content-Type: multipart/alternative; boundary=089e014953ecf2b9b5050ea16141 Subject: Re: [PHP-DEV] Design by Contract From: yohgaki@ohgaki.net (Yasuo Ohgaki) --089e014953ecf2b9b5050ea16141 Content-Type: text/plain; charset=UTF-8 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 --089e014953ecf2b9b5050ea16141--