Page 1 of 2
=& and function(&$param)
Posted: Tue Aug 14, 2007 10:36 pm
by s.dot
I've read all about references. I just don't understand
when and
why and when i'd
need to use them.
I've seen this code...
Code: Select all
require_once 'test.class.php';
$test =& new test();
$test->a = $b;
Why wouldn't a regular
= work?
And in functions...
Posted: Tue Aug 14, 2007 11:44 pm
by feyd
It would copy the object, potentially creating extra memory demand that isn't necessary. Basically, if you don't need a copy of the object created, use a reference. PHP will use a reference automatically provided you don't modify the variable/object.. but as soon as you do modify it, it'll get copied if you use the basic assignment operator.
Posted: Tue Aug 14, 2007 11:49 pm
by s.dot
So, for functions. If you're just "passing something along", use function(&$bar) to avoid using more memory?
I can't see the point in objects, since, as far as I've used them they've always been modified. I mean, why instantiate an object if you don't plan to set some properties? (I guess for using public static methods?)
Posted: Tue Aug 14, 2007 11:52 pm
by s.dot
o0o wait.
Code: Select all
require_once 'class.test.php';
$test = new test();
//set property a
$test->a = $b;
//this will provide a clean copy of test() object without $test->a set??
$test2 =& new test();
Posted: Wed Aug 15, 2007 12:07 am
by feyd
$test2 wouldn't have the "a" property in either way of assignment.
Code: Select all
$test = new test();
$test->a = 'wee';
$test2 =& $test;
$test2->b = '??';
$test3 = $test;
$test3->b = 'burger';
var_dump($test, $test2, $test3);
may illustrate the differences better.
Re: =& and function(&$param)
Posted: Wed Aug 15, 2007 1:12 am
by Christopher
scottayy wrote:Why wouldn't a regular = work?
The short answer is that in PHP4 you need to user =& when you create a new instance of an object and use &$param when you pass an object to a function in order to get objects to behave as you would expect.
The problem is that in PHP4 objects are really just a special type of associative array. Think about what is happening in an assignment statement:
Code: Select all
$object = new MyClass();
// is the same in PHP4 as:
$foo = $bar;
The
new keyword creates an object varable on the right side of the equation. It is a normal variable like scalars. When you assign that newly created variable to $object it copies the value that is returned by the
new keyword and assigns it to $object. So memory-wise you have two copies of the object -- one returned by the
new keyword, and the other that was copied to the $object variable.
Now if you do:
Then $object is a reference to the variable returned by the
new keyword, so only one object is created.
Of course in PHP5 objects are managed by handle, not like regular variables, so when you assign or pass then you are always assigning or passing a handle, not the variable itself.
Posted: Wed Aug 15, 2007 1:23 am
by s.dot
So in PHP5, I don't have to worry about creating references?
Posted: Wed Aug 15, 2007 1:39 am
by Christopher
scottayy wrote:So in PHP5, I don't have to worry about creating references?
Correct, because object vars in PHP5 are handles to the actual object (which is a different thing than references). Think of them more like file or database resource links.
Posted: Wed Aug 15, 2007 5:01 am
by VladSun
All you need to know about when and how to use references can be found here:
http://www.php.net/references
Thank you for asking this question - it made me read this manual and I found that my understanding of "PHP reference" was very mistaken (i.e. reference in PHP is not like reference in C++).

Posted: Wed Aug 15, 2007 9:19 am
by superdezign
VladSun wrote:it made me read this manual and I found that my understanding of "PHP reference" was very mistaken (i.e. reference in PHP is not like reference in C++).

That's one thing about PHP and C++. They use the same terms, but PHP's versions of them (Exceptions, function overloading, etc.) are different. Yet, since so many of us are familiar with other high-level programming languages, we assume it's the same until we take the time to read those few short chapters at the start of the manual.
Posted: Wed Aug 15, 2007 10:57 am
by s.dot
So when passing a reference as a function parameter, you're not really saving any memory by creating the reference, you're just creating an extra pointer to it. Again, I don't see the point.. (unless this was a php4 thing as well).
Posted: Wed Aug 15, 2007 11:10 am
by VladSun
I think that in this case you can reword this as passing argument by address, not by value. I.e. if you change some of the properties of the argument inside the function scope it get also changed outside the function scope.
Posted: Wed Aug 15, 2007 11:12 am
by superdezign
scottayy wrote:Again, I don't see the point..
For objects, there is no point because you've already got it in PHP5. However, for arrays and anything scalar, references give you access to the actual data. There will be times when you need to alter the actual data in a separate function.
Posted: Wed Aug 15, 2007 11:13 am
by VladSun
In the link I've pointed to you, they said that even "global var1, var2" declarations in a function scope are indeed references to the variables with global scope.
Posted: Wed Aug 15, 2007 11:14 am
by Christopher
scottayy wrote:So when passing a reference as a function parameter, you're not really saving any memory by creating the reference, you're just creating an extra pointer to it. Again, I don't see the point.. (unless this was a php4 thing as well).
Actually you are saving memory because by passing by reference you are only using the memory for a reference as opposed to creating a whole copy of the object passed -- which is what you do in PHP4 if you do not use a reference. And because you are making a copy when you pass a normal parameter, the changes you make to an object will not be reflected outside the function.