Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:112944 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 17531 invoked from network); 20 Jan 2021 20:19:44 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 20 Jan 2021 20:19:44 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 790001804AA for ; Wed, 20 Jan 2021 11:59:46 -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=1.4 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_SOFTFAIL autolearn=no autolearn_force=no version=3.4.2 X-Spam-Virus: No X-Envelope-From: Received: from mail-lj1-f177.google.com (mail-lj1-f177.google.com [209.85.208.177]) (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 ; Wed, 20 Jan 2021 11:59:45 -0800 (PST) Received: by mail-lj1-f177.google.com with SMTP id n11so27537864lji.5 for ; Wed, 20 Jan 2021 11:59:45 -0800 (PST) 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=ejJ3nOPbONQC/3EMr4HKndSv7RJ2OMaHjIYaMO0ZdhM=; b=Uw5uHFOiTZ39lQzjhUI1HLD8r3coc01IjwCjP2vAuiVUNH+KhRMGGFDPDFNcv5/m9P ++9rlP5tadF21f4FKUGgA5MbB5Mp4Ojxh52D6WCEbU3K/bgOnpixi1EjCe6fL9zEI7I5 voWaoghNnr90lL5mrxUdJRbwIyFxv+BQqbEUaOVfvtRkrPrh6Tfvz/qrSsYUGrITgBGW wxM5IVe15UU0rCWn/xabUcS72a5BzHt66GjV352SrC7dlIuvmtFBwQL/Lrt5kgY15wkj ExzmnC08j5GEM6yS4+VAnOsZUANoZMVVvAZ8dO1rt1m3Qx0jgQYs3B95zeOwo4OA7BYt bx1Q== X-Gm-Message-State: AOAM532wNf+zTrMZ1QzYYgb9ahl4GBL4Lj4c4Gs3Mq/txHuDqi0f5r7R n2p+anjKamAFICTN2jju7VOb5A98+76uJFbT8qJinw== X-Google-Smtp-Source: ABdhPJyIz/DTGatfLqOEhlJGwhnaxCdCUM5NmXfjsHFevbWHHg3GBfF7QJIiw7B2VzxKxMdKogdM/eE701kO5HTy4eo= X-Received: by 2002:a05:651c:1308:: with SMTP id u8mr4499051lja.365.1611172780419; Wed, 20 Jan 2021 11:59:40 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 20 Jan 2021 13:59:29 -0600 Message-ID: To: Nikita Popov Cc: tyson andre , "internals@lists.php.net" Content-Type: multipart/alternative; boundary="0000000000003fba1805b95a6584" Subject: Re: [PHP-DEV] Proposal: short_var_export($value, bool $return=false, int $flags=0) From: pollita@php.net (Sara Golemon) --0000000000003fba1805b95a6584 Content-Type: text/plain; charset="UTF-8" On Tue, Jan 19, 2021 at 3:59 AM Nikita Popov wrote: > * 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. > I'd argue the opposite. If dumping a particularly large tree of elements, serializing that to a single string before then being able to write it to file or wherever seems like packing on a lot of unnecessary effort. What I would do is expand the purpose of the $output parameter to take a stream. STDOUT by default, a file stream for writing to include files (one of the more common uses), or even a tmpfile() if you do actually want it in a var. ** Or.... See my comment about objects further down. > * 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 > > I would also make `var_export` the dominant part of the name, so `var_export_SOMETHING()`. Alternatively how about making a VarExporter class. $exporter = new VarExporter; // Defaults to basic set of encoding options TBD $exporter->setIndent(' '); // 2 spaces, 1 tab, whatever blows your dress up $exporter->setUserShortArray(false); // e.g. use array(...) etc... $serialized = $exporter->serialize($var); // Exports to a var $exporter->serializeToFile($var, '/tmp/include.inc'); // Exports to a file $exporter->serializeToStream($var, $stream); // Exports to an already open stream And if you want the defaults, then just: $serialized = (var VarExporter)->serialize($var); Potentially, one could also allow overriding helper methods to perform transformations along the way: // VarExporter which encodes all strings as base64 blobs. class Base64StringVarExporter extends VarExporter { public function encodeString(string $var): string { // parent behavior is `return '"' . addslashes($var) . '"'; return "base64_decode('" . base64_encode($var) . "')"; } } Not the most performant thing, but extremely powerful. -Sara --0000000000003fba1805b95a6584--