Re: Methods which auto-return the class instance

From: Date: Wed, 28 Dec 2022 06:53:57 +0000
Subject: Re: Methods which auto-return the class instance
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-119211@lists.php.net to get a copy of this message
On Fri, Dec 23, 2022, 7:46 PM joke2k <joke2k@gmail.com> wrote: > Thanks for the feedback! > > On Fri, 23 Dec 2022, 09:33 Claude Pache, <claude.pache@gmail.com> wrote: > > > It is very common for fluent class methods to have a verbose `return > > $this;` ending in their body. > > But If you have declared self as return type into a > > non-static class > > method you have only two options to return: > > > > - the $this object > > - another instance of the same class or subclass > > > > ... in order to avoid a return type error. > > > > > > It is still two options, and it is not clear in general which one to > pick. > > You could also say that, if you have object as return > > type, there are > two > > options to avoid a return type error: either return the 9Ú > > ÿÏɈ©S˜ñjruÒ·$this object, > or > > return another object. > > > > Yes but declaring self is more strict than > object. > And talking about the word itself... Declaring to return (your-)KÈ > ôòÚ}�’XN2XNself but > you could return someone-else of the same type as you. > But it is ok, it refers to the class name not the instance. just semantic. > I would rather endure it returns the used instance and not another one with a compatible interface. Not doing do is actually confusing to me, as per the meaning of self. . Or maybe, if the goal is to ease declaration of fluent interfaces (methods chaining), declaring a class fluent (annotation?) would achieve the same with ambiguous names? >

« previous php.internals (#119211) next »