Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112925 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67943 invoked from network); 19 Jan 2021 10:19:55 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 19 Jan 2021 10:19:55 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 3BEDA1804DC for ; Tue, 19 Jan 2021 01:59:36 -0800 (PST) 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,HTML_MESSAGE, 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-lf1-f41.google.com (mail-lf1-f41.google.com [209.85.167.41]) (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 ; Tue, 19 Jan 2021 01:59:35 -0800 (PST) Received: by mail-lf1-f41.google.com with SMTP id q12so1196192lfo.12 for ; Tue, 19 Jan 2021 01:59:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sxIm5dewikIVDln7iLbMf3nsxa22YBb4G7tFBJmTQM0=; b=GFg3wkjMCmlfdfYSjWXEv6yxaLyphsblXEpRS5b4sqNF0Rtmeh5WEMKXlI4UoVA5RU ISq2b/LIMJIbBu5w8AI2IlRD+H0m3yaLiu92HrjoTK5bc8vYRbxCLZmBY9fCsH0DHT6A +07gVnGvgkXidctR4PfXAHPfGRKVFozbWpgBeqkn3CukzJtEkBy9khGlwdHeJsgbvyQ5 s4jyyVmWw58vCHxYMOiP8nfvAfJu5ltVyekAadLUvN1R/4+D6OnxyH04FgEKg53eY0LU 79SM5z1oVpqpw13OxeDa2CK/Iy1Fgf5syEUiTph+YlHoLtudK+raUStGGu3lkbaOYQsG MaUw== 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=sxIm5dewikIVDln7iLbMf3nsxa22YBb4G7tFBJmTQM0=; b=BEutkWZmL/cX1M8GhoVUtLZZRA5Ahw5XKLziMKLV0FGJldoAGCKJQ4uMu8/n9JtfO4 mi95xv/nlfnPXfTZDjgUrpad1NZMVMCD7qiYjDa0P40Kdv5RKWP2c51vIRPq2cIEIVZu QmahCfYgPoD8WISzQxQNQGhdNZ5K0f6nBU0eSk/nmDGtJTZlAhOm/5uN1RcnNiy0Be9L 3RI25jMgvy2R0EIOOk6Yq0zWWIq9i4XoaJR326dBdB88GbIM8wWusEAvaP/9K/OPTJbB NEA5IQkRdRox4+Sw2GxrE3zUWs2zN9Pw5YbFhUkZSA9jcXbfRE+E0UKty9DCBMsIYvqD eEbw== X-Gm-Message-State: AOAM53117oURdJZWnFcbl5x8GNo1dsyXBF+q6QcdbsNuspjiKcPtfxMM niYbub1SAdZysub/DwiiTcE5w5EaBqTH6nSy9V0= X-Google-Smtp-Source: ABdhPJz9tZzmpIkOzog6UlTrYb6cWSwwps/fwpyjh+bAasi/B7MLhdgmfCnYuOC8pibZE6PoZ38GTJNy4xkNu9j02dw= X-Received: by 2002:ac2:4431:: with SMTP id w17mr1450537lfl.223.1611050373174; Tue, 19 Jan 2021 01:59:33 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Tue, 19 Jan 2021 10:59:17 +0100 Message-ID: To: tyson andre Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary="000000000000355ae305b93de56e" Subject: Re: [PHP-DEV] Proposal: short_var_export($value, bool $return=false, int $flags=0) From: nikita.ppv@gmail.com (Nikita Popov) --000000000000355ae305b93de56e Content-Type: text/plain; charset="UTF-8" On Mon, Jan 18, 2021 at 10:12 PM tyson andre wrote: > Hi internals, > > I've created a PR https://github.com/php/php-src/pull/6619 to add an > alternative to var_export(). > The proposed short_var_export() also outputs/returns a parseable > representation of a variable, > but adds requested features for var_export. > > 1. Use `null` instead of `NULL` - the former is recommended by more coding > guidelines (https://www.php-fig.org/psr/psr-2/). > 2. Change the way indentation is done for arrays/objects. > See ext/standard/tests/general_functions/short_var_export1.phpt > (e.g. always indent with 2 spaces, never 3 in objects, and put the > array start on the > same line as the key) > 3. Render lists as `"[\n 'item1',\n]"` rather than `"array(\n 0 => > 'item1',\n)"` > 4. Prepend `\` to class names so that generated code snippets can be used > in > namespaces (e.g. with eval()) without any issues. > 5. Support opt-in `SHORT_VAR_EXPORT_SINGLE_LINE` in `int $flags`. > This will use a single-line representation for arrays/objects, though > strings with embedded newlines will still cause newlines in the output. > (`"['item']"`) > > Changing var_export() itself has been proposed 9 months ago ( > https://externals.io/message/109415) > but the RFC discussion seems to be on hold. > https://externals.io/message/106674#106684 suggested adding a new > function as an alternative. > One of the major objections is that changing var_export() (instead of > adding a new function) would have these drawbacks: > > 1. It would be impractical to backport/polyfill calls with 3 args to older > php versions, if `int $flags` was added. > ArgumentCountErrors are thrown in php 8.0 for too many parameters, and > earlier php versions warn > 2. If defaults were changed, then thousands of unit tests in php-src and > other projects may fail due to > tests expecting the exact representation of a variable, requiring a lot > of work to update tests > > I feel like the user-friendliness of the enhancements makes adding another > function > to PHP worth it. (e.g. copying short_var_export() into a code snippet such > as the expectation for a unit test or a configuration) > I don't believe I've seen an RFC for a separate function before. > > Also, a native C implementation would be much more efficient than a > polyfill implemented in PHP, > e.g. https://github.com/brick/varexporter/issues/14 > > I plan to start an RFC document shortly. Thoughts? > (e.g. for the name, short_var_export() seemed more appropriate than > var_export_short(), pretty_var_export() or canonical_var_export()) > > Thanks, > - Tyson > Hi Tyson, The formatting of var_export is certainly a recurring complaint, and previous discussions were not particularly open to changing current var_export behavior, so adding a new function seems to be the way to address the issue (the alternative would be to add a flag to var_export). I like the idea of the "one line" flag. Actually, this is the main part I'm interested in :) With the one line flag, this produces the ideal formatting for PHPT tests that want to print something like "$v1 + $v2 = $v3". None of our current dumping functions are suitable for this purpose (json_encode comes closest, but has edge cases like lack of NAN support.) Some note: * You should drop the $return parameter and make it always return. As this is primarily an export and not a dumping function, printing to stdout doesn't make sense to me. * For strings, have you considered printing them as double-quoted and escaping more characters? This would avoid newlines in oneline mode. And would allow you to escape more control characters. I also find the current '' . "\0" . '' format for encoding null bytes quite awkward. * I don't like the short_var_export() name. Is "short" really the primary characteristic of this function? Both var_export_pretty and var_export_canonical seem better to me, though I can't say they're great either. I will refrain from proposing real_var_export() ... oops :P Regards, Nikita --000000000000355ae305b93de56e--