Article

The PHP Anthology Volume 1, Chapter 2 - Object Oriented PHP

Page: 1 2 3 4 5 6 7 8 9 10 11 Next

Good and Bad Practices

When working with classes and objects, it’s a good idea to use references whenever an object is involved. Occasionally, you may have to do the same with an array, such as when you want to sort the array in a different section of code. But, for the most part, normal variables will not need this treatment, simply because, when your code reaches the level of complexity where you’d need to do so, you will (I hope!) be storing variables inside objects and passing the complete object by reference.

Let’s look at some other situations in which you might need to use references…

// Make sure $myObject is a reference to      
// the variable created by the new keyword      
$myObject = &new MyClass();

This looks odd at first, but remember, a variable created by the new keyword is being passed here—even if you can’t see it. The reference operator saves PHP from having to create a copy of the newly-created object to store in $myObject.

class Bar {      
}      
     
class Foo {      
 // Return by reference      
 function &getBar()      
 {      
   return new Bar();      
 }      
}      
     
// Instantiate Foo      
$foo = &new Foo();      
     
// Get an instance of Bar from Foo      
$bar = &$foo->getBar();

In the above example, you’ll notice the getBar method in the Foo class. By preceding the function name with the reference operator, the value the function returns is passed by reference. Note that we also had to use a reference operator when assigning the return value of getBar to $bar. This technique is commonly used when a class method will return objects.

What’s bad practice is the following:

function display($message) {      
 echo $message;      
}      
     
$myMessage = 'Hello World!';      
     
// Call time pass by reference - bad practice!      
display(&$message);

That’s known as a call-time pass-by-reference, which PHP controls with the following setting in php.ini:

allow_call_time_pass_reference = Off

By default, in recent PHP releases the above setting should be switched to Off; turning it on is “frowned upon” by PHP’s makers. Switched off, PHP will generate warning errors every time a function call specifies an argument should be passed by reference. As such, it’s good practice to leave this setting off.

The reason why call time pass by reference is a “bad thing” is that call time passing by reference can make code extremely difficult to follow. I’ve occasionally seen PHP XML parsers written using a call-time pass-by-reference—it’s nearly impossible to gain any idea of what’s going on.

The “decision” as to whether a variable is passed by reference or not is one that belongs to the function being called, not the code that calls it. The above code written correctly would look like this:

// Accept by reference - good practice      
function display(&$message)      
{      
 echo $message;      
}      
     
$myMessage = 'Hello World!';      
     
display($message);

Performance Issues

Depending on the scale of your application, there are some performance issues you might need to consider when using references.

In simple cases of copying one variable to another PHP's internal reference counting feature prevents unnecessary memory usage. For example,

$a = 'the quick brown fox';      
$b = $a;

In the above example, the value of $b would not take up any extra memory, as PHP’s internal reference counting will implicitly reference $b and $a to the same location in memory, until their values become different. This is an internal feature of PHP and affects performance without affecting behavior. We don’t need to worry about it much.

In some cases, however, using a reference is faster, especially with large arrays and objects, where PHP's internal reference counting can’t be used. and the contents must therefore be copied.

So, for best performance, you should do the following:

  • With simple values such as integers and strings, avoid references whenever possible.
  • With complex values such as arrays and objects, use references whenever possible.

References and PHP 5

With PHP 5, references will cease to be an issue because the default behavior of PHP, when passing objects, will be to pass by reference. If you ever need a copy of an object, you can use the special __clone method to create copies.

Essentially, the change brings PHP in line with the majority of object oriented programming languages like Java, and will certainly do a lot to reduce the confusion surrounding the subject. For now, though, and until PHP 5 has been widely adopted, knowing how references work is important.

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links