Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:101888 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 26985 invoked from network); 20 Feb 2018 09:42:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Feb 2018 09:42:52 -0000 Authentication-Results: pb1.pair.com header.from=derick@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=derick@php.net; spf=unknown; sender-id=unknown Received-SPF: unknown (pb1.pair.com: domain php.net does not designate 82.113.146.227 as permitted sender) X-PHP-List-Original-Sender: derick@php.net X-Host-Fingerprint: 82.113.146.227 xdebug.org Received: from [82.113.146.227] ([82.113.146.227:52206] helo=xdebug.org) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CD/E3-22623-79DEB8A5 for ; Tue, 20 Feb 2018 04:42:49 -0500 Received: from localhost (localhost [IPv6:::1]) by xdebug.org (Postfix) with ESMTPS id 3B24210D32C; Tue, 20 Feb 2018 09:42:44 +0000 (GMT) Date: Tue, 20 Feb 2018 09:42:43 +0000 (GMT) X-X-Sender: derick@singlemalt.home.derickrethans.nl To: Andreas Hennings cc: PHP internals In-Reply-To: Message-ID: References: User-Agent: Alpine 2.21 (DEB 202 2017-01-01) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Subject: Re: [PHP-DEV] var_export() array format From: derick@php.net (Derick Rethans) On Sun, 18 Feb 2018, Andreas Hennings wrote: > 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. To iterate the purpose of var_export(): Output a variable structure in such a way that PHP can read it back in by parsing it. Anything else, is just being nice. > 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 As opinions are going to differ on what is the "right way", expect that part of the discussion to turn into bike-shedding. > ## 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 think this is all a big overkill, especially when you take into the account the purpose of var_export. cheers, Derick -- https://derickrethans.nl | https://xdebug.org | https://dram.io Like Xdebug? Consider a donation: https://xdebug.org/donate.php, or become my Patron: https://www.patreon.com/derickr twitter: @derickr and @xdebug