Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:81578 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 11121 invoked from network); 2 Feb 2015 10:02:55 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 Feb 2015 10:02:55 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.182 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.182 mail-wi0-f182.google.com Received: from [209.85.212.182] ([209.85.212.182:64348] helo=mail-wi0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B1/D8-02376-E4B4FC45 for ; Mon, 02 Feb 2015 05:02:55 -0500 Received: by mail-wi0-f182.google.com with SMTP id n3so13954632wiv.3 for ; Mon, 02 Feb 2015 02:02:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=WA5eJhAX1tSNAz/1IEyZ+UlFGNni9pbmebkzYSHh4yA=; b=dhyCKTrHquq2QOSCmLXWaDb/PS9eSysOQujlkzjrhoKrlOPYUobRdtybzPjLTMW+hx +Jw/pmgJ8QJZMcqChR+QQA21SPY+oDxXdALoq44KP94xCeKUemks4IBU0wD2Ol3jxvct dRl3WmtZMtdgf7V/nm2UtJDCsr8FnEH4+ZWdeVDj43lhsab2WDHf3cwgFC8ik7ZiG/Xp Jarmy6W5ZQufIp2R2/0AreNmdOdRWWAndPSTm4GXMMqBiWJK6dUpAQ03j9b5nI2bsXmS ol3X3uThSx9qcLI2ASxoh8+GdeAwp1B6HiqjmpXrT7xaptc8HuAQ/GvPA7liWe8PLRTJ Ep+A== X-Received: by 10.180.95.162 with SMTP id dl2mr22870515wib.31.1422871371143; Mon, 02 Feb 2015 02:02:51 -0800 (PST) Received: from [192.168.0.172] ([62.189.198.114]) by mx.google.com with ESMTPSA id uo6sm27112005wjc.49.2015.02.02.02.02.49 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 02 Feb 2015 02:02:50 -0800 (PST) Message-ID: <54CF4B14.4080806@gmail.com> Date: Mon, 02 Feb 2015 10:01:56 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 MIME-Version: 1.0 To: internals@lists.php.net References: <54CBC804.7050706@gmail.com> <54CD7668.30301@garfieldtech.com> <61B0AAF8-9A3E-4889-917E-42AA48D946FA@ajf.me> <54CF4126.8030901@lsces.co.uk> In-Reply-To: <54CF4126.8030901@lsces.co.uk> Content-Type: multipart/alternative; boundary="------------050900040602040009080300" Subject: Re: [PHP-DEV] [RFC] Immutable variables and objects From: rowan.collins@gmail.com (Rowan Collins) --------------050900040602040009080300 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit 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] || --------------050900040602040009080300--