Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81683 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27882 invoked from network); 3 Feb 2015 06:59:13 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Feb 2015 06:59:13 -0000 Authentication-Results: pb1.pair.com smtp.mail=lisachenko.it@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=lisachenko.it@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.174 as permitted sender) X-PHP-List-Original-Sender: lisachenko.it@gmail.com X-Host-Fingerprint: 74.125.82.174 mail-we0-f174.google.com Received: from [74.125.82.174] ([74.125.82.174:33115] helo=mail-we0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 74/51-20608-EB170D45 for ; Tue, 03 Feb 2015 01:59:11 -0500 Received: by mail-we0-f174.google.com with SMTP id w55so37802447wes.5 for ; Mon, 02 Feb 2015 22:59:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=0Y5PXc5rZY0fdy511ztV8zibkQG8sTCLMlDpbQEPrlI=; b=RDXq9H9/zufvqXrFjKY0aIJcLeBLmglE4YGSNSn+nDPREl4eGvvi097Oro42E0YWes ChKaTurvCJngE0OBLk7WyNtD85vlNK0MTj8OQ1hdnkCglu3LhlWugIO1yNd2HaaMPAkn 4b7xbATdlTTUsz4l24vy0zc1QAYhediBV//LcQpa+omBNcNhoIizl0lyZn/MlByQevOH O+3jYaBEavqhdva6+7AbE/iVMV4LK1h8asZ05BAzzkS49pJwHNi+lXegPOhBQHWNdQG7 E/yAQL6TphEaN1DTironjrhILotUfNgZ8LNrpFy2ALQV6r4+FSKMwo6INWF+XuSNMD93 ysLA== MIME-Version: 1.0 X-Received: by 10.180.218.73 with SMTP id pe9mr32010992wic.13.1422946747010; Mon, 02 Feb 2015 22:59:07 -0800 (PST) Received: by 10.194.154.229 with HTTP; Mon, 2 Feb 2015 22:59:06 -0800 (PST) In-Reply-To: <54CF4B14.4080806@gmail.com> References: <54CBC804.7050706@gmail.com> <54CD7668.30301@garfieldtech.com> <61B0AAF8-9A3E-4889-917E-42AA48D946FA@ajf.me> <54CF4126.8030901@lsces.co.uk> <54CF4B14.4080806@gmail.com> Date: Tue, 3 Feb 2015 09:59:06 +0300 Message-ID: To: Rowan Collins Cc: PHP internals list Content-Type: multipart/alternative; boundary=001a1134dedc63d55c050e299b1c Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: lisachenko.it@gmail.com (Alexander Lisachenko) --001a1134dedc63d55c050e299b1c Content-Type: text/plain; charset=UTF-8 I want to add a link here to the Java article about Value Types, this information is quite interesting: http://cr.openjdk.java.net/~jrose/values/values-0.html Probably, immutable value objects will be soon in Java world according to this information: > Conclusion > > Along with the questions, we believe we have enough answers and insights > to begin prototyping value types, and verifying our thesis. That is, we > think it quite likely that a restricted class-like type can be made to look > enough like a primitive to be worth adding to the language and VM. > 2015-02-02 13:01 GMT+03:00 Rowan Collins : > Lester Caine wrote on 02/02/2015 09:19: > >> This is probably because I still don't understand objects, as I still >> just consider them as arrays with a few more complex elements. I STILL >> work on the basis that is I pass by reference, I can make modifications >> to the data while if I don't I get a copy and have to pass the copy back >> if I need the original changed. This used to be all very simple, so when >> did it stop working? This may explain why I get into trouble with stuff >> that has been working for years but after 'modernising' starts throwing >> problems :( >> > > Since PHP 5, and in most other languages, objects are passed with an extra > level of indirection, almost but not quite the same as pass-by-reference. > So there are actually three ways a variable can be passed (or assigned): > > - scalar by value - an assignment to the variable changes only that > variable > - scalar by reference, using & - both variable names point to the same > variable, and assigning a value to one assigns it to both > - object by value - an assignment to the variable still only changes that > variable, BUT two variables can point at the same object can both modify > its internal state. So if you have $current_user = getCurrentUserObject(); > then saying $current_user = null; is changing its value, so would only > change that variable, but $current_user->setPassword('1234'); is not, so > the change would be visible from all variables pointing at that object. > > Immutable parameters for scalars and arrays are simple: currently you can > use a by-value parameter as a local variable in your function, but you > might want to avoid the confusion of this: function foo( const $bar ) { > $bar = 42; // ERROR } or foo( const array $bar ) { $bar[] = 42; // ERROR } > > Immutable parameters for objects are complicated, because foo( const $bar > ) { $bar->value = 42; } is not technically changing the *value* of that > object, but it would be useful to have an enforcement that it wouldn't > happen. > > Hope that clarifies things a bit. > > Regards, > -- > Rowan Collins > [IMSoP] > || > --001a1134dedc63d55c050e299b1c--