=& and function(&$param)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

=& and function(&$param)

Post 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...

Code: Select all

function foo(&$bar)
{
    //
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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();
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: =& and function(&$param)

Post 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:

Code: Select all

$object =& new MyClass();
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.
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

So in PHP5, I don't have to worry about creating references?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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++). :)
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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).
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
Last edited by superdezign on Wed Aug 15, 2007 11:13 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Post Reply