Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:109427 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 81097 invoked from network); 29 Mar 2020 22:51:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Mar 2020 22:51:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4A3211804C2 for ; Sun, 29 Mar 2020 14:16:56 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 29 Mar 2020 14:16:55 -0700 (PDT) Received: by mail-lj1-f170.google.com with SMTP id p14so15882784lji.11 for ; Sun, 29 Mar 2020 14:16:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=qC5M2vH2XSHKCuPzrL8b3PeqcrpLBSsHiu9AZoPPdN0=; b=OIw0QwKJ67lzZITx7Gj3kzOaBD07aoztiRfdOtkB6zVXrSDq6XE0+Np7qonGl5oCi9 U9CvxgJgkkYxSBkf2Aya3og5Ob18n80spJ5foQOIqwiz586VKOxNh5Hty1k+EX42nETw /H/Gt9dkwy4BWs6qyuddO+AhGwkyxS46b1uQxcudrOcLuS8G7LpswxwTcq3d3W3FR17U PwX1hIM3m+id+79QpX8o2C0+NC/XJygRbnkTh13xF0pQlBRrVgcPwtUw59oRCWayXmbm uxkLpBMp7Sj8/oac5izQrhZ0UxWGZH6yxiSMTI+odH7UJkjDW8lnstqjH9UTC/wkiY+Y ZOqQ== X-Gm-Message-State: AGi0PuaIv4wErXA6VxDRwsaO6dWYoWobaK8ZXMnTOtTT3z+SKivwZc9/ nGOHfA4W5QL5eQw4mcX+NjX4rfgmp0y8kdjbHvWwLuu/neg= X-Google-Smtp-Source: APiQypLXlX3PfhGlLu76L2YS4yGVRoqi6nPLEf4dsXjRar8qSVq/lW2b/VyqWFuefzm590o01O0rYG4skXn1WA99aNU= X-Received: by 2002:a05:651c:383:: with SMTP id e3mr5105855ljp.271.1585516612603; Sun, 29 Mar 2020 14:16:52 -0700 (PDT) MIME-Version: 1.0 References: <5e80bf4d.1c69fb81.14ece.c957SMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: <5e80bf4d.1c69fb81.14ece.c957SMTPIN_ADDED_MISSING@mx.google.com> Date: Sun, 29 Mar 2020 16:16:41 -0500 Message-ID: To: Andrea Faulds Cc: php internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC] [DISCUSSION] Compact Object Property Assignment From: jakob@givoni.dk (Jakob Givoni) Hi Andrea, On Sun, Mar 29, 2020 at 10:31 AM Andrea Faulds wrote: > > > (function ($obj) { > $foo = "foo"; > $bar = "bar"; > $baz = "baz"; > > $vars = get_defined_vars(); > foreach ($vars as $key => $value) { > $obj->$key = $value; > } > })($object); > > It's not very elegant though, and unfortunately you can't move the > get_defined_vars() part to another function. In an early version of my RFC I had included a long section of examples of how you could do what COPA does but with current syntax, each with its flaws. I've tried everything from magic methods, anonymous classes and closures :-) I removed it because it didn't seem to add much to the discussion and the RFC became too long for a thorough read. Thanks for the creativity though, I like playing with ideas like that, but I must confess I frown upon functions like get_defined_vars() and extract(), if you know what I mean :-D I'd like to iterate that I believe the force of COPA is it's simplicity - and I think it's gone unnoticed for many that the only thing COPA adds to existing syntax is a set of square brackets around the assignments. Everything else is familiar. I repeat, the only difference between these two snippets is the [...]: ------------------ $myObj->a = 1; $myObj->b = 2; $myObj->c = 3; ------------------ $myObj->[ a = 1, b = 2, c = 3, ]; ----------------