Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102224 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 61589 invoked from network); 11 Jun 2018 09:12:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 11 Jun 2018 09:12:33 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.52 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.52 mail-wm0-f52.google.com Received: from [74.125.82.52] ([74.125.82.52:38102] helo=mail-wm0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6F/EB-62758-FFC3E1B5 for ; Mon, 11 Jun 2018 05:12:31 -0400 Received: by mail-wm0-f52.google.com with SMTP id 69-v6so14947896wmf.3 for ; Mon, 11 Jun 2018 02:12:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=pydKk+epYSR8sUJCmZ3974oUNcsD3OXQ/2Guy4ceJ94=; b=R0CPEBXUhyHjvr36CaITXhufTywL6pEQ4Uh+YURI7pNTgWX9X+0+r/iGH6EZX9P0F7 vtzvPRxTnUZ60sEYImeZw4e3aLDNE/ttvkmEUGWqRRUbC9ABrQ6UNSHRGc03PFjbcj64 moz+uh7DY4p7/Gmv0TPbpyqAKUxqqtAuNCem5oN6hMiFR/yL3eKIFj/lLeGC1Addj4QV 05iDt6wGbsv6k5CSUgk4BTTfs/qL3ISNAiOlVTIdMbrJatMig3EXnQ6ssGS4QEfGYJyo /gOodR1PpSPlgBNFvjzI5whgjUfhEsyJ+FyFjKELo8ra6WxP9zFZTeaHBlUTCzUCjlPZ gZdw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=pydKk+epYSR8sUJCmZ3974oUNcsD3OXQ/2Guy4ceJ94=; b=R9MWXng6q03EKh0XGmsqaYYbprbjKdAklWlaKGdnigYpS+G62MfwZ1wYCSqJibKEeJ aCuPUlCSPZs3glN296bvYOf3f/cltYoLL+MzDa3x8dzErsJkYaO7Fr8XGN6L1xYaVSY0 9tqcB9wUag3BordKVdpxdEtY+wKevLwXplP68SPbrUs9SRXAVn1Xj1mMcq67H9HF93Gg 4SC0aEzb5GwBzK/nPe+Rn3nzgpu3PnTD3CAm3x18tPpEs0z94+Bexx8zfF+a3O87CpUL kSnof0sVW7cil/mKKho9DaplzIlVjU4iEP8mYZou1xkRlvC04afDThEwBNX2QpXF8DGr 4yaA== X-Gm-Message-State: APt69E3Mt59c6R9CSEEbeYPYONjw2TtEqgZmMvxkDnTJ73hObU34nEMS XGR7EakL85mtTXbT9/NzYZg5p58Vsp/wG7P+8uWRYg== X-Google-Smtp-Source: ADUXVKK3veTy1oRa/gB1oqPZU6eoHqDw5PMSL4J/ie7lc/dPzCqjYsX+LOQT+0ztdHR6ZhrmzQXRBFoQSkZOuznsk9M= X-Received: by 2002:a50:95f0:: with SMTP id x45-v6mr18228255eda.99.1528708348323; Mon, 11 Jun 2018 02:12:28 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a50:8d55:0:0:0:0:0 with HTTP; Mon, 11 Jun 2018 02:12:27 -0700 (PDT) In-Reply-To: <1528697673100.21360@ku.edu> References: <1528656103200.14318@ku.edu> <1528697673100.21360@ku.edu> Date: Mon, 11 Jun 2018 10:12:27 +0100 Message-ID: To: PHP internals Content-Type: multipart/alternative; boundary="000000000000112c38056e5a26ec" Subject: Re: [PHP-DEV] [VOTE] Make compact function reports undefined passed variables From: rowan.collins@gmail.com (Rowan Collins) --000000000000112c38056e5a26ec Content-Type: text/plain; charset="UTF-8" On 11 June 2018 at 07:14, Hoffman, Zachary Robert wrote: > > However, even if those 4 values end up being stored in a structure, I > don't want to manipulate that structure within that class method itself, I > want to perform computations with local variables. If I have to separately > store those variables to the structure at the end of the method, that is > fine. > If the variables have to share a name with the fields in the structure, they're not really local variables - you can't rename them, add more intermediate steps, etc, without breaking their magic affinity with the named fields. > I end up using `compact()` in the same way when rendering Blade views from > Laravel controllers, which is easier to demonstrate, so I made a small > Laravel project to demonstrate `compact()` being useful. It is viewable at > https://gitlab.com/zrhoffman/compact-example . The project only consists > of 2 short files (which are copied into a Laravel base install), which I > will paste here anyway. > I can see why this code is tempting, but it looks very "closely coupled" to me: *why* are the local variables in the controller named the same thing as in the view? What if the word "power" is ambiguous in the context of the controller, and so you want to rename that variable $exponent? Because you've assumed you can use compact(), you can't do that without tracing the variable through the whole application - these are now effectively global variables, rather than local ones. We could still avoid it on our end by rewriting the return statement of the > Laravel route to this: > > return view('index', [ > 'power' => $power, > 'sum' => $sum, > 'xor' => $xor, > 'base' => $base, > ]); > > Since I am necessarily expecting those variable names on the other side > anyway, this seems like extra effort worth avoiding. > The fact that the keys and values look so similar here is a coincidence of the current implementation, and making that explicit is not wasted effort. If you refactor the code and rename the local variable $power to $exponent, even the most naive refactoring tool will give you working code: return view('index', [ 'power' => $exponent, 'sum' => $sum, 'xor' => $xor, 'base' => $base, ]); This is a huge advantage in maintainability of the code - it's easier to understand, harder to break, and more able to evolve. > The other reason I mentioned Laravel is that storing variables by key in > an array and extracting them later is intrinsic to Laravel's process for > rendering views, and writing "fundamentally dynamic code" is pretty much > inescapable in this case. > Yes, I've seen Blade (and templating generally) as a justification for dynamic variable names before, and I can see the appeal of reusing the PHP engine as the templating engine, with a different set of variables in scope. It could, however, be considered syntax sugar rather than truly dynamic variable names - a template compiler could convert "$foo" into "$params['foo']", just as it converts other Blade-specific language features. Regards, -- Rowan Collins [IMSoP] --000000000000112c38056e5a26ec--