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 -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php