Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101884 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 92774 invoked from network); 18 Feb 2018 01:04:19 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Feb 2018 01:04:19 -0000 Authentication-Results: pb1.pair.com smtp.mail=david.proweb@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=david.proweb@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.43 as permitted sender) X-PHP-List-Original-Sender: david.proweb@gmail.com X-Host-Fingerprint: 209.85.214.43 mail-it0-f43.google.com Received: from [209.85.214.43] ([209.85.214.43:38037] helo=mail-it0-f43.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9A/A5-26725-011D88A5 for ; Sat, 17 Feb 2018 20:04:16 -0500 Received: by mail-it0-f43.google.com with SMTP id l129so1490430ita.3 for ; Sat, 17 Feb 2018 17:04:16 -0800 (PST) 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 :cc; bh=R3zalPbPR5sKToOHjrs5SL4tIq5pSZJW9SBG86zdmVw=; b=qIgGMLJCO7MyBhJO+KvCnMRopA6D1FMvwUimaOi2HD0ix69VmqSkqn+hepR7xcEMrK jYUD4gkHt4SoT5YspTidE+bcuWd0+1v0UuP9SMeOU6hyjjxNw0ML+5qeV4OKLgnPoT9B g8CJtqJu8u+4kR3ew6IrKZ2wE67S4/miH1cMx6MvETo35LLBPmFJ56+PdcSQNnfTlbGA nJWMvaq9RYZbx0t4CNBDDBnh5enMJrNAODGjCk8DFUC23neRpsRG2BepCLW/Xbkifaca rpvoBKnNdI2cQJ1p6hUlGSvVsNjY04szRbOQOb06r4nP9y3eJgQQXGB65CTIc7aDPp0A CIZQ== 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:cc; bh=R3zalPbPR5sKToOHjrs5SL4tIq5pSZJW9SBG86zdmVw=; b=Tyau7HZB8DjHlQPQwbtOR4CYzhQQm8ja3bDKnGD+Amv71KFC/2x3qpN1yozQVy7DAL IQRDL82c+902ors8S5BVIdpDZ5LTmo3aq2QcPqawfETjUmg0cqManrFjUdoZexCjU0fs nCF1MOgG4r9BCB9WF2sgR15qedu2bsDghOfhTR9q0p+UB65hunZscPTK6sOmMrKsTCKw GTLzw/SBhSEuQVYd5LdhD3qhfgAZBJWezejZ42wGZTZfeZvgAfHs7MfN05XAkbDRls+a 5OaafXZS5unrBIdgPf0HCuRm8bJJ7/yeddDA9kVNZR7mHxUnm8eh5Zb9A8+YVm/br0hi +9/A== X-Gm-Message-State: APf1xPB8tCaS+Pg9YXWbjZwEtvZUIP3cqUEEt/lPibere6V2kOkPKPtx YhSM0FkJGk82d4lgiCNpd7RAE/iM2BfPkSjmcbk= X-Google-Smtp-Source: AH8x2243rf2KIju4Gwpqott8gVmzQpHEwtjZmRwCun7EzW2JodEcSxplUvqoUoG6mEGBnwqPU8XntP2kZ6KKSmO4zT0= X-Received: by 10.36.107.134 with SMTP id v128mr15004214itc.39.1518915853897; Sat, 17 Feb 2018 17:04:13 -0800 (PST) MIME-Version: 1.0 Received: by 10.2.48.211 with HTTP; Sat, 17 Feb 2018 17:04:12 -0800 (PST) Received: by 10.2.48.211 with HTTP; Sat, 17 Feb 2018 17:04:12 -0800 (PST) In-Reply-To: References: Date: Sat, 17 Feb 2018 23:04:12 -0200 Message-ID: To: Andreas Hennings Cc: PHP Internals Content-Type: multipart/alternative; boundary="001a114a9bf8ea553a05657227ff" Subject: Re: [PHP-DEV] var_export() array format From: david.proweb@gmail.com (David Rodrigues) --001a114a9bf8ea553a05657227ff Content-Type: text/plain; charset="UTF-8" I just think that is not responsability of PHP to care about output format at core level (although we have the json pretty output support...). Maybe you can found some solution with packages. Em 17 de fev de 2018 10:28 PM, "Andreas Hennings" escreveu: > 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 > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --001a114a9bf8ea553a05657227ff--