References in PHP

May 17, 2007 on 3:10 am | In PHP |

References in PHP are important!

This is something I never paid much attention to in the past. I only started using references in my current project. I know for a fact that many less advanced PHP coders never use references, because on the php.net documentation contribution notes, there are actually people who advocate never using references because they are “confusing”.

References are not confusing at all! They work in PHP exactly as they do in C and C++. Keep in mind that references are not pointers, and if you don’t know what references are, I suggest you look it up in a tutorial elsewhere, because I won’t start explaining it, as that will probably take a whole other entry.

Function arguments: Pass-by-reference?

References are especially important in passing function arguments. Of course, you all know that to modify variables that you pass in within a function, you have to pass by reference. But that’s not the only case! Passing by references actually improves performances. You save the computer an extra step of making a copy of the variable. You may not want to do that for typical strings, integers since they are quick to copy (it’s probably safer to pass a copy in those cases — you don’t want to accidentally change their values if they are not meant to be changed); however with your own objects, you might really want to do that. Think about a case like:

class CustomType {
   ... // a hundred member variables
   ... // and a good number of member methods
}
$obj = new CustomType;
foobar($obj);

In this case, if you declare foobar normally, when the function is called, the entire object is copied for the function to use. If your script is executed million times a second in a high traffic site, you can improve performance greatly by simplying declaring your function this way:

function foobar(&$myObj) {
   ...
}

It is true that the function will be able to modify the object (so be careful!), but this will optimize your code by quite a bit.

Call-time pass-by-reference

But there’s something you want to watch out for: when passing by reference, have your function take in arguments as reference. That is, declare your function foobar like above, and when you call it, simply pass a regular variable to it.

If you do it the other way around, like this:

function foobar($myObj) {
   ...
}
$obj = new CustomType;
foobar(&$obj);

This is called call-time pass-by-reference. And this is bad!

This is because whether a function argument should be passed by reference, should be a decision that belongs to the function — not the code that calls it. In fact, in the PHP configuration (php.ini), there is a setting:

allow_call_time_pass_reference = Off

It is set to off by default, so that whenever you do something like above, it produces a warning on the output. It serves as a reminder to tell you that it is bad!

Some links to look at..

Don’t know references? PHP.net has some documentation about it. Learn to use references, your code could become much more efficient!

No Comments yet »

RSS feed for comments on this post. TrackBack URI

Leave a comment

You must be logged in to post a comment.

Powered by WordPress with Pool theme design by Borja Fernandez. Entries and comments feeds. Valid XHTML and CSS. ^Top^