note 43779 deleted from language.oop.newref by danbrown

From: Date: Sat, 07 Nov 2009 19:11:11 +0000
Subject: note 43779 deleted from language.oop.newref by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-162585@lists.php.net to get a copy of this message
Note Submitter: phpnotes at robertdhill dot com ---- An earler note asked why the equal operator doesn't just make a reference, assuming that a direct reference would always be desired. The answer is because a direct reference is *not* always desired. When you instantiate a class, you are creating an object that has its own copy of all the class variables, access to the class methods, etc. Now say you've created an instance of a class "MyClass" called "myObjectA" and you've done some processing to it, set some variables or whatever. Now say you want to create a duplicate of the object to do some changes, but you want to *keep the original.* That is what the assignment (=) operator is for. If you'd made a reference (&=), then any changes you make to the duplicate are also reflected in the original. This is undesireable. So there are circumstances where the assignment (=) operator is required, and there are circumstances where the reference (&=) operator is required. It just depends on what you're doing. Hope this helps.

« previous php.notes (#162585) next »