Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:93662 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 6637 invoked from network); 1 Jun 2016 02:15:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jun 2016 02:15:48 -0000 Authentication-Results: pb1.pair.com smtp.mail=jesseschalken@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=jesseschalken@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.223.180 as permitted sender) X-PHP-List-Original-Sender: jesseschalken@gmail.com X-Host-Fingerprint: 209.85.223.180 mail-io0-f180.google.com Received: from [209.85.223.180] ([209.85.223.180:35068] helo=mail-io0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2D/A0-30737-3554E475 for ; Tue, 31 May 2016 22:15:47 -0400 Received: by mail-io0-f180.google.com with SMTP id p64so8767510ioi.2 for ; Tue, 31 May 2016 19:15:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:message-id:subject:from:to; bh=yWqXGxbZP2k5twjgHl/lar83iZiWKE90/VzCizMpTLs=; b=lsc8FezooyGniw137dvrM0hXnfRr4eS9cJrfxOBfGz9QO9/vZ9zegdnGs2Mcbd+xJu urtwXB1vEBAqGbeEYr+x7bEcfhDxVd8YO7wWTmqXkF2xPd14xPjVLmTWuhowubwvtV1W hApvhrYtabVPQvEDciwD57AAazjT8ThBEcJ+5SFNyBzbX3GtBMMIu6DNHldPTvPp9xkf JtrIo1bXvWDYmPfzhYj7Rg+e5mv6Pbp/P3a3zWzavhfXCW0or+eVe0dkN1Kpt8/BmdTL 8aQOLVZvP483nSM3y2J3N/1Ux0Qb3rJeOsZm6YQpgMVuh65S3dVf78jYUQzjxwDGN+3l 3huw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:date:message-id:subject:from :to; bh=yWqXGxbZP2k5twjgHl/lar83iZiWKE90/VzCizMpTLs=; b=LZmfFO56rdaJuzDYMwpUTJrLTOCjAiZxXD6FztP4KE0IGLci/ZKoI9gX1PCN5jwPeK AqjC6X/D+4ZnpNq9Gfi1Un3DXqe4QV7NhodgPtBIn1tcJx8NfPoT1RiSjOWkGTPq4cQI pHtwjL8NPwfP6+zYMfvDT8GgsGrrDYUhQk9n51kOQ7ljkXPzymOhF/hj6rQD1s+nVj1o +C37joy/1DozJJ+nw5aPFqhekM5+zOid7eySAaEd/NeKRj6pkl74sTPATZ24Ue+P4dy9 q8yvWEjkh712zLjahU6FHiI456dqTuMdQ4so3ryGPHjnhPIYP04X3FhBA9rl2p52GEZq ZYEw== X-Gm-Message-State: ALyK8tITxMZrzMeVNqOHPDnhvq4A0Zf/EpYJHovVlh/CuVqfINbHwhIXOeXo4IYqtqJrCsy0v71rw4xsRiSrRA== MIME-Version: 1.0 X-Received: by 10.107.137.232 with SMTP id t101mr2096677ioi.31.1464747344520; Tue, 31 May 2016 19:15:44 -0700 (PDT) Sender: jesseschalken@gmail.com Received: by 10.79.111.208 with HTTP; Tue, 31 May 2016 19:15:44 -0700 (PDT) Date: Wed, 1 Jun 2016 12:15:44 +1000 X-Google-Sender-Auth: 5TaH18gFgAEgsH7RV4VKsSLudsk Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary=001a113fbc08280d2c05342e1125 Subject: Set object properties inline From: me@jesseschalken.com (Jesse Schalken) --001a113fbc08280d2c05342e1125 Content-Type: text/plain; charset=UTF-8 Hi internals, I often have code dealing with plain old PHP objects with properties and no methods, either as a substitute for keyword arguments, or to represent a JSON or YAML document for a web service, configuration file or schemaless database. At the moment, instantiating an object and setting public properties requires a temporary variable for each object in the structure: $obj1 = new Obj1(); $obj1->prop1 = ...; $obj1->prop2 = ...; $params = new FooParams(); $params->prop1 = ..; $params->prop2 = ...; $params->prop3 = $obj1; $this->fooMethod($arg1, $arg2, $params); For large structures, this gets verbose very quick. There is a good example of this here involving 18 unnecessarily variables. I can remove the local variables by defining setters for all the properties: $this->fooMethod( $arg1, $arg2, (new FooParams()) ->setProp1(...) ->setProp2(...) ->setProp3((new Obj1()) ->setProp1(...) ->setProp2(...)) ); But now for each property I have to spend an extra 3-5 lines of code defining a setter, which is more code than it saved (unless the class is used heavily enough). I could define __construct() taking every property as a parameter, but then each property has to be mentioned another three times (four times with a doc comment), and the type twice: class FooParams { public int $prop1; // ... public function __construct( int $prop1 // ... ) { $this->prop1 = $prop1; // ... } } and where the object is constructed, it isn't immediately visible what the meaning of each positional parameter is without some IDE assistance (eg Ctrl+P in PhpStorm), and only specifying some parameters requires filling preceding ones with defaults. I could also define the __call() method to automatically expose setters, but then IDEs and static analysis can't understand what's going on (it can't see that those methods exist, what their parameters are and what they return). @method doc comments on the class help, but that's another line for every property which I have to manually keep in sync with the real properties. It would be great if there was a simple shorthand syntax for setting properties on an object in-line, without needing to extract a variable: $this->fooMethod( $arg1, $arg2, new FooParams() { prop1 = ..., prop2 = ..., prop3 = new Obj1() { prop1 = ..., prop2 = ..., }, } ); This way the structure can be written directly in the code as an expression and FooParams and Obj1 remain simple containers for properties. The grammar might look like (I haven't used bison/yacc before): expr_without_variable: /* ... */ | expr '{' inline_set_properties '}' ; inline_set_properties: /* empty */ | identifier '=' expr | identifier '=' expr ',' inline_set_properties ; (Although I think that would conflict with the alternative $var{8} syntax for array/string offset.) Has this been explored before? What problems can you foresee (or have been foreseen) with such a feature? Thanks --001a113fbc08280d2c05342e1125--