Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101883 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90486 invoked from network); 18 Feb 2018 00:28:15 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Feb 2018 00:28:15 -0000 Authentication-Results: pb1.pair.com header.from=andreas@dqxtech.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=andreas@dqxtech.net; spf=permerror; sender-id=unknown Received-SPF: error (pb1.pair.com: domain dqxtech.net from 209.85.215.42 cause and error) X-PHP-List-Original-Sender: andreas@dqxtech.net X-Host-Fingerprint: 209.85.215.42 mail-lf0-f42.google.com Received: from [209.85.215.42] ([209.85.215.42:38522] helo=mail-lf0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9D/45-26725-D98C88A5 for ; Sat, 17 Feb 2018 19:28:14 -0500 Received: by mail-lf0-f42.google.com with SMTP id g72so8646409lfg.5 for ; Sat, 17 Feb 2018 16:28:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=pobdT+tQ0sj/MrY6Db5nNa8I1nO5lITmIc2SnIh+3UE=; b=B78ZK2y5towYK57rXzT/qef9Pw9oB774wlEQJkNvdXzwzh/4yeXTnk9wCJ3WiT8Wzw btgT7g3WQnpGgFYerPsmcm+/bDh7ECsK7DSIFDUkMMFCQjiwsK9FuBP27MRl/53mDume qpwYlCyRbsSpEPdEkTS6KZ/3LSViqkdax6fdaVdWQ+4rn//uSDnzY/TGIiSTTDDuAkbq Ssj22BcKLNWBgXag+HWZCDUt/FLRhxKiDJwXKQaP15ZRYAci2CrEdO4OjpZ/V9M3D+7D sdpP8cK/eE6KWp7AdOZjR99TvJaotzVUW/GOPIO+UHttaTtnHyvthAmajxKmy8NzBH5U EhEA== 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=pobdT+tQ0sj/MrY6Db5nNa8I1nO5lITmIc2SnIh+3UE=; b=dtXL10dcMCcolZ8Kaydu/rQrLHYiHFut69WpnmJormBHIszpELDq6YmTevB4eR+Tjk 3yMvUWGK4TyAkqEgtkJcjv09mrS/NfXMrusMkbnZsjjkgmtnFSZw9H5VPeyQ2PGVcRka Pya1RNl5mJTEzHN1KRQpZT5TmtZBJXdSKFbDAH5Ys5z5/8ekzRe9Va+xTjJCFqCZeOTh 96QgjmylCAC2loinxjnD0U07MPWJAgHzSdRxf1lE3c+koBZwiMWX/ySQPIBrhBQtXE78 XsWNyddYZKagkjvVVqGFBQH8QUfZks9XsyzYJrBbJX3sY/QhqqymzdMdnCFSftjtuzdg 6/ZQ== X-Gm-Message-State: APf1xPCxilqdPUWw8VBZT69gqKIZJYYbxSvpf04HGdjkONkuQgM7NPJC 0S/ajHvilXQN1AampPGTkWVbBY1l X-Google-Smtp-Source: AH8x227iBm2KJz8EBQcsd729FmVlvLqcX3uPUC2+EBlUWdEDx3NEsNlIQ8xTmqQqea+QkNIu6UsJdw== X-Received: by 10.46.73.81 with SMTP id b17mr7352328ljd.122.1518913690425; Sat, 17 Feb 2018 16:28:10 -0800 (PST) Received: from mail-lf0-f44.google.com (mail-lf0-f44.google.com. [209.85.215.44]) by smtp.googlemail.com with ESMTPSA id y7sm2159424ljd.45.2018.02.17.16.28.08 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sat, 17 Feb 2018 16:28:09 -0800 (PST) Received: by mail-lf0-f44.google.com with SMTP id g72so8646346lfg.5 for ; Sat, 17 Feb 2018 16:28:08 -0800 (PST) X-Received: by 10.25.153.200 with SMTP id b191mr6713481lfe.73.1518913688461; Sat, 17 Feb 2018 16:28:08 -0800 (PST) MIME-Version: 1.0 Received: by 10.25.225.78 with HTTP; Sat, 17 Feb 2018 16:27:47 -0800 (PST) Date: Sun, 18 Feb 2018 01:27:47 +0100 X-Gmail-Original-Message-ID: Message-ID: To: PHP internals Content-Type: text/plain; charset="UTF-8" Subject: var_export() array format From: andreas@dqxtech.net (Andreas Hennings) I sometimes use var_export() to export a structured array and then copy+paste it into my code. And sometimes I use it as part of an automatic code generator. Unfortunately, the output is usually not in the format that I want, or that is common for most projects / code styles. - Indentation is 2 spaces instead of 4, as in most projects. - It uses traditional "array()" format, instead of "[]". - It puts a space after "array", so "array (" instead of "array(". - It puts "array (" on a new line after "=>", instead of "'key' => array(" on one line. - Empty arrays occupy two lines instead of one, so "array (\n)" instead of "array ()". E.g. https://3v4l.org/nl4uJ The IDE can fix some of these problems with a single operation. Others require cumbersome manual treatment. E.g. my IDE (PhpStorm) cannot automatically remove the line break between "=>" and "array (". Code generators can be designed to fix all of the problems, but imo they should not have to deal with this stuff. ------ ## Proposed change I really think the default behavior of var_export() must remain unchanged for BC. Re-running the same script in different PHP versions should produce the same output each time. What we can do is add a third parameter with flags. Then we can introduce named flag combinations for known code styles. Flag constants could be - VAR_EXPORT_INDENT_TAB - VAR_EXPORT_INDENT_2 - VAR_EXPORT_INDENT_3 - VAR_EXPORT_INDENT_4 - VAR_EXPORT_ARRAY_SHORT_SYNTAX for "[]" instead of "array (..)". - VAR_EXPORT_ARRAY_NO_INITIAL_BREAK for " => array (" instead of " =>\n array(". - VAR_EXPORT_ARRAY_NO_INITIAL_SPACE for "array(" instead of "array (". - VAR_EXPORT_ARRAY_ONELINE_IF_EMPTY From other discussions: - VAR_EXPORT_STDCLASS_AS_CAST for "(object)array (.." instead of "stdClass::__set_state(array(..))". - VAR_EXPORT_FQCN for "\stdClass" and "\Acme\Animal" instead of "stdClass" and "Acme\Animal". The naming is open for debate, of course. I prefixed all array-related constants with *_ARRAY_* so that they group up more nicely. I considered for a moment to prefix the constants with "PHP_" or something else instead of "VAR_EXPORT_", so they could be used elsewhere in the future. ------ For a short time I considered to mix all this into the second parameter which currently only accepts boolean. Once you use any non-zero flags, it would always return. For my taste, the print-by-default is a bad idea anyway. But I now think a third parameter would be more transparent and clean. The indentation could be solved with a 4th parameter instead of *_INDENT_* constants. See https://externals.io/message/51345#51351 Another alternative would be to introduce yet another function which always returns, has a more useful default format, and where the second parameter accepts flags like mentioned above. But somehow it feels wrong to me to introduce such a new function which mostly does the same as an already existing one. But most of them are quite specific to var_export(), and would not make sense elsewhere. ------ Why not a userland implementation? E.g. https://packagist.org/packages/riimu/kit-phpencoder var_export() is often used in experimental code or when just playing around, situations where developers usually prefer to avoid adding a 3rd party dependency. Also there is no way to add a 3rd party dependency on e.g. 3v4l.org. And a custom one-off implementation is not what developers should spend their time on (although I did it a few times already). ----- -- Andreas