Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:94459 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 82463 invoked from network); 10 Jul 2016 10:34:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 10 Jul 2016 10:34:40 -0000 Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.213.45 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.213.45 mail-vk0-f45.google.com Received: from [209.85.213.45] ([209.85.213.45:35275] helo=mail-vk0-f45.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A9/E2-46733-FB422875 for ; Sun, 10 Jul 2016 06:34:39 -0400 Received: by mail-vk0-f45.google.com with SMTP id v6so105810514vkb.2 for ; Sun, 10 Jul 2016 03:34:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=mindplay-dk.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=J8M7kkIcx4/FsrowTi7qIIkIDgdaBNOVnEH8TVmod4Y=; b=lGqvmSsip0ArYmY0MUcX2NQDwB/NzB+IiufoFjK4X0gymuOdSJ7/MnD9jpmhhTi8p1 0fQVyrzgfj2R+xyOPxeMR5YsDr+ypbGeqnvNy+GTSXOzwkFHI37lPzqOEjn1DAid0+GU 68dXmOme+baZtGGGj3m7K6Qs+Pws83V/jjMOOmkJFHuRTaKBMBusgoZtZ+nxOFosz39H B+dEMITXXFa8Zlb3urLoN1mxBJh9TXOjPUE4/G9aPGzITP8kMy4EK3DkgVHn5ZvKsawm Le7P/AfQb4ulj8Csp6GVZ7138XOpf6/K/aNSa7pdmLqWl/vbF2xzRaF3qXy6ls+sFac2 642w== 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:from:date :message-id:subject:to:cc; bh=J8M7kkIcx4/FsrowTi7qIIkIDgdaBNOVnEH8TVmod4Y=; b=gYGU3aCyitR0Sd1W75kQufyeWyB5MYB37XCf2ixKYF5tLE8eesEJZwIxZonHaJu3Ji WbCxbICD50EbpsaOJz7yNrM4o2I4VA+8E9Jjiz19oFgQ7lZxpWROBEf+3sLyZW481am2 OkNX9uVYMabj1PF1T3G3vbMYqlT/YrRf+5GEBYN5e1US/yT/ov6fGBXqrPoGnWHKOy4f 7fhe8mtN8CHMcP8JHN6tVqZGvje73feNdnQXCXChV0dNmnlxy0aiJe0pzBMH6unTI6cV LnvHMejv7/8bdcPXlKm6xnV3CHwPRxL0OvINigNnICovtZlOrluWKdBUgfm5SHAYERwJ iNsQ== X-Gm-Message-State: ALyK8tKNPXDB6us44NwY4Yr3QF9zCw79aBEGajWmkPMFUNaJvXHnXIBv+Z+G/a0avLeHZ4MfN2m4z8RJVBRDqg== X-Received: by 10.159.41.164 with SMTP id s33mr6565122uas.146.1468146876452; Sun, 10 Jul 2016 03:34:36 -0700 (PDT) MIME-Version: 1.0 Received: by 10.103.105.9 with HTTP; Sun, 10 Jul 2016 03:34:35 -0700 (PDT) In-Reply-To: References: Date: Sun, 10 Jul 2016 12:34:35 +0200 Message-ID: To: Daniel Ciochiu Cc: PHP internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Proposal for php7 class method return From: rasmus@mindplay.dk (Rasmus Schultz) Just my opinion, but I view method chaining as a poor man's work-around to a missing (and much more versatile) language feature, and would therefore rather not see us adding a feature that inspires what is, in my opinion, and anti-pattern. Functions are intended as a means of returning the result of an operation - when you return the object itself, that's not the result of an operation, so this is highly non-idiomatic use of a very fundamental language feature, and it clouds the role of functions and return-values. What you're really trying to accomplish is something like the ".." operator found in some other languages - this is known as the "cascade operator" in Dart, for example: http://news.dartlang.org/2012/02/method-cascades-in-dart-posted-by-gilad.html There are several problems with method-chaining - the biggest problem, as demonstrated by the cascade operator, is that this doesn't need to be a problem you solve for every class; it can be done systemically with a cascade operator, e.g. supported for every class, not just those where you weave in this requirement. Also, method chaining is inferior to a cascade operator, which would work not only for methods, but for properties as well. More over, a cascade operator would work for all existing libraries, whether the designer thought about cascading use of various features or not. In short, this shouldn't and doesn't need to be a problem every developer solves for each and every library - we can solve it for the language instead. In other words, it seems you're trying to address a problem that shouldn't exist in the first place, so that you can solve a recurring problem you should not have to solve at all. I would be much more interested in seeing a proposal for a cascade operator, which, in my opinion, addresses the real problem currently driving developers to use method-chaining - I can only view that as an unfortunate work-around. In my opinion, a cascade operator would move the language forward, while the addition of a magical type-hint acting as sugar for "return $this" merely solves a superficial problem - and propagates a bad practice. On Sat, Jul 9, 2016 at 8:47 AM, Daniel Ciochiu wrote: > Hi, > > I have a proposal that for a given method with a return type of , if > method does not return anything than it should it's instance. It will > reduce code by only one line, but will improve consecutive method calls. > > class myClass > { > protected $value; > > function setValue($value) : self > { > $this->value = $value; > } > > function doubleValue() : self > { > $this->value*=2; > } > > function getValue() : self > { > return $this->value; > } > } > > $calculator = new myClass(); > > $returnValue = $calculator->setValue(3) > ->doubleValue() > ->getValue(); > > echo $returnValue; > > Any return defined in the methods body would override the return of the > class instance. > > I am looking forward to your reaction. > Thank you! > > DanielCiochiu