Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81523 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64691 invoked from network); 1 Feb 2015 08:55:34 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Feb 2015 08:55:34 -0000 Authentication-Results: pb1.pair.com smtp.mail=cryptocompress@googlemail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=cryptocompress@googlemail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain googlemail.com designates 74.125.82.179 as permitted sender) X-PHP-List-Original-Sender: cryptocompress@googlemail.com X-Host-Fingerprint: 74.125.82.179 mail-we0-f179.google.com Received: from [74.125.82.179] ([74.125.82.179:33932] helo=mail-we0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 76/03-01632-40AEDC45 for ; Sun, 01 Feb 2015 03:55:34 -0500 Received: by mail-we0-f179.google.com with SMTP id q59so33973000wes.10 for ; Sun, 01 Feb 2015 00:55:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=googlemail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=JSKsMzEAHW+nEEt2IRJFn7uiGDKpcd2t8REDARZ29OU=; b=HoNAJlqAYvj0Lb0R9Fh8ZZwYP/PO+fyIupik5GUT0y/2E9MrNZticIttC7rJFpcGlN Ygra5V/Ym4mmD9iqAXgRwtX7fWxh1xOailsdjiVTxxuMGFW2niDSfXMbrUMzuz1iSv73 W31UmTzyct/oa7tmsDIcjB5Sv6h35IyqgmT8OKvDlFZh/i9PYgfuytLMGh2lLtVxjpt7 Mn/GWxlwudygQ8Oh25uaRMpxtmd1o4LuaTfEpS2xrnbdpcOG5tjf/MWSsBAiq/taPzD/ vp394qVZ2dx5ECxXMiPAajfCiYm4xUhQqxrWAnTwQI3LJXaOoId4ippouHt56Ek/+bQr KjzQ== X-Received: by 10.180.73.239 with SMTP id o15mr12284969wiv.14.1422780929826; Sun, 01 Feb 2015 00:55:29 -0800 (PST) Received: from [192.168.1.115] (mnch-5d868c4b.pool.mediaWays.net. [93.134.140.75]) by mx.google.com with ESMTPSA id g8sm10127688wiy.6.2015.02.01.00.55.28 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 01 Feb 2015 00:55:28 -0800 (PST) Message-ID: <54CDE9FF.40700@googlemail.com> Date: Sun, 01 Feb 2015 09:55:27 +0100 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: PHP Developers Mailing List References: <54CBC804.7050706@gmail.com> <54CD7668.30301@garfieldtech.com> <54CD7975.8040908@gmail.com> In-Reply-To: <54CD7975.8040908@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: cryptocompress@googlemail.com (Crypto Compress) >> The with*() methods in PSR-7 are documented to return a new instance, >> not modify the existing instance. Yes, there's no way in PHP itself to >> force that syntactically, which is why documentation exists. :-) >> >> Also, in the benchmarks we've run the performance cost of all those new >> objects is measured in nanoseconds, ie, small enough that we're not >> worried about it. (Hats off to the PHP Internals folks for making that >> fast!) > It is great that this is fast, but I wonder (maybe off-topic?) why do > it? I.e. it is clear that in something like: > > $a = new Request->withHeaders(...)->withBody(...) > ->withEncoding(...)->withETag(...) > > the intermediate objects are useless and nobody needs 5 new objects when > you do it. Am I missing something here? Hi, my assumptions after some testing: - This is only true in case of reassigning ($a = clone $a) the old variable, as refcount for underlying values remains 1 for all values in clones, so cow don't need to copy anything. - If the old object is not thrown away, then memory consumption is doubled and the "fast" argument is wrong. (Performance, of cloning an object without copying values and of some method calls, is negligible.) Omit mutator methods (with*/set*) is a simple and logical way to achieve immutability in PHP. No need for const/final/...