Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:111519 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 619 invoked from network); 14 Aug 2020 17:14:19 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 14 Aug 2020 17:14:19 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3A65A18055B for ; Fri, 14 Aug 2020 09:14:27 -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=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) (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 ; Fri, 14 Aug 2020 09:14:26 -0700 (PDT) Received: by mail-ed1-f46.google.com with SMTP id df16so7223413edb.9 for ; Fri, 14 Aug 2020 09:14:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=1JNzwvjkxpy31gy6jOw7DBs7gmZAQsQdo0Dt6swTkNk=; b=ncF/YKtHE9FY0IskvVtkPVHUUohIaAQFknCyll/JSvRv64MCw7Ti1Cg60paXTMdRB2 4XjJm+TTB4Q5R2qRqVKi5EfWITUWh+CVRTfLYQaNag8AsArGZfYmEZFuKsmHT3EHW+b+ NpU7n4Au3dJpP7llwC6KdBlKh6CB0YaImq1sJQ6mMU/XqIXlGnOvFE+ksynkhfHDcWG4 r5aVB4adrRlaTGWgS0ct7efGagqVM74JFtxodA86Sn1jKff0ebf4AQX7rmBGabWL04+1 RAZCdBVjao+Y5gHEYGNpTzn0IhXUz8hq1BiFjH3ct79xqTnAoRBhOIF6IjnB0qsdpay2 oCdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=1JNzwvjkxpy31gy6jOw7DBs7gmZAQsQdo0Dt6swTkNk=; b=ds66uKLhSwrjm/xw7X0Yn7sdx5gvm5L2gj8YfvvtOP/YPaRJwowAVRi9IdO0WpZdSt CP91g1FVHquZlxetqs4jPEj+vKMcuUk+MNWqaquxod7uecdtCJoWahvoFuOw8tE2U2cn pwaPUPQIefduYlY/fW3hk7nEhiXRoV/kO1qm5PXgqvht94H5QtYgmk/16ZyvNprIAcS4 fPoF4J8t4wBEtbHVsQ9aRlT39auv2bjH7rbwpP9roYh1RygY6CB7ardDiMUevsOV+XMW W76Qm/mjX1as/rVHgd2uV0g5CO+LENr9YIcOWcNb7NEjFPFVxDxZkkv5YemJbTvWKfCH L1rw== X-Gm-Message-State: AOAM531LOErQQ6sFWoomQUFo55L0PZVHb42nR8tRu7MaXLvpBOfxLtn8 2dHVYlHgLDmWgxJbuJF14/CQMvxOStIfaYZbzy2O9DPumLc= X-Google-Smtp-Source: ABdhPJxDqUTu8tKRTnXj9AWrdNUQo7Gp+lC5M5ZGjt/v0GN5yRIlOv1up3lkyImQDAMg7ennDxnDghdEpdvhoPiAnMc= X-Received: by 2002:aa7:da8c:: with SMTP id q12mr3074853eds.126.1597421661504; Fri, 14 Aug 2020 09:14:21 -0700 (PDT) MIME-Version: 1.0 Date: Fri, 14 Aug 2020 18:14:10 +0200 Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: [RFC] Better string interpolation From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi internals I've been thinking about ways to improve string interpolation. String interpolation in PHP is currently pretty limited to say the least. What's worse is we have several ways to do the same thing and a few of them being quite inconsistent and confusing. We have these syntaxes: 1. Directly embedding variables: "$foo" 2. Braces outside the variable "{$foo}" 3. Braces after the dollar sign: "${foo}" 4. Dynamic variable lookup: "${expr}" The difference between 3 and 4 is extra confusing. Passing a simple variable name without a dollar inside the braces (and optionally an offset access) will embed the given variable in the string. When passing a more complex expression ${} will behave completely differently and perform a dynamic variable lookup. https://3v4l.org/uqcjf Let's look at some different examples. Simple local variables work well, although it is questionable why we have 4 ways to do the same thing. 1. "$foo" 2. "{$foo}" 3. "${foo}" 4. "${'foo'}" Accessing offsets is supported for syntax 1, 2 and 3. String quoting in the offset expression is inconsistent. 1. "$foo[bar]" 2. "{$foo['bar']}" 3. "${foo['bar']}" Accessing properties is supported for syntax 1 and 2. 1. "$foo->bar" 2. "{$foo->bar}" Nested property fetches or offset accesses only work with syntax 2. 1. "$foo[bar][baz]" // [baz] is not part of the expression, equivalent to "{$foo['bar']}[baz]" 2. "{$foo['bar']['baz']}" 3. "${foo['bar']['baz']}" // Syntax error Calling methods is only supported for syntax 2. 1. "$foo->bar()" // Treats ->bar as a property access, equivalent to "{$foo->bar}()" 2. "{$foo->bar()}" Arbitrary expressions work for none of the syntaxes. 2. * "{$foo + 2}" // Syntax error * "{Foo::bar()}" // Interpreted as string The most functional of these syntaxes is 2 but even here only a small subset of expressions are accepted. The distinction between syntax 3 and 4 is very confusing and we should probably deprecate and remove syntax 3 (as it does pretty much the same as syntax 2). Sadly, the only syntax that accepts arbitrary expressions (4) is also the least useful. As to allowing arbitrary string interpolation, we have three options: 1. Deprecate syntax 4, remove it, and change its functionality in the future 2. Use some new syntax (e.g. "\(1 + 2)", "\{1 + 2}", "$(1 + 2)", etc) with the given BC break 3. Do nothing Adding new syntax will break many regular expressions and embedded jQuery code, although they are easily fixed by escaping the \ or $. Deprecating, removing and then changing ${} would take many years. I don't know which of these is the better approach. Any thoughts or different ideas? Ilija