Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have some C++ knowledge and know that pointers are commonly used there, but I've started to look at PHP open source code and I never see code using references in methods.

Instead, the code always uses a return value instead of passing the reference to the variable to the method, which then changes that variable's value and just returns it.

I have read that using references uses less memory, so why aren't they used in PHP?

share|improve this question

migrated from stackoverflow.com Feb 28 '13 at 16:24

This question came from our site for professional and enthusiast programmers.

15  
Using pass-by-reference parameters is "side-effect programming" - using a function to return a single value is significantly clearer when reading code. That said, quite a few PHP core functions use references. –  halfer Feb 28 '13 at 15:58
3  
PHP passes objects by reference by default. –  DCoder Feb 28 '13 at 16:04
2  
array sorts are a prime exanmple of core PHP functions that pass by reference –  Mark Baker Feb 28 '13 at 16:05
2  
PHP references are not pointers, and are often a pain in the ass if you aren't fully expecting them where they appear. –  lanzz Feb 28 '13 at 16:07
2  
If you need multiple returns, return array($foo, $bar); and list($A, $B) = foobar(); –  Izkata Feb 28 '13 at 17:35

7 Answers 7

up vote 24 down vote accepted

Your assertion that references are rarely used is incorrect. As others have already mentioned there's a ton of native functions that use references, notable examples include the array sorting functions and preg_match() / preg_match_all(). If you are using any of these functions in your code, you are also using references.

Moving on, references in PHP are not pointers. Since you're coming from a C++ background I can understand the confusion, but PHP references are an entirely different beast, they are aliases to a symbol table. Any performance gains you might have expected from C++ references simply don't apply to PHP references.

In fact, in most scenarios passing by value is faster and less memory intensive than passing by reference. The Zend Engine, PHP's core, uses a copy-on-write optimization mechanism that does not create a copy of a variable until it is modified. Passing by reference usually breaks the copy-on-write pattern and requires a copy whether you modify the value or not.

Don't be afraid to use references in PHP when you need to, but don't just do it as an attempt to micro-optimize. Remember, premature optimization is the root of all evil.

Further reading:

share|improve this answer
1  
Great answer!!! –  eskimo Feb 28 '13 at 23:46
    
Thanks for the correction @NikiC. Bit of a lazy answer this one... –  Yannis Rizos Mar 2 '13 at 9:26
    
To complete the answer, it'd be nice to point out that all objects are passed by reference in PHP. –  Florian Margaine Mar 2 '13 at 9:40
1  
@FlorianMargaine No, the two are very different and it's important to realize why that is so. –  phant0m Mar 2 '13 at 12:05
1  
@FlorianMargaine No, suppose objects were passed by reference in PHP: f($obj) given function f($x) { $x = new X; }, would have $obj refer to a different object than before the call to f(). However, since objects aren't passed by reference, $obj isn't modified if you were to execute this with PHP. In that way, PHP does the same as Java. –  phant0m Apr 23 '13 at 9:27

PHP is a web-oriented language.
Web-pages being served fast and should be lightweight.
Usual PHP program lives fraction of second and consume few hundred kilobytes of memory.
There is no use for such optimizations.

share|improve this answer
1  
This. Doesn't need pointers/references, as it isn't messing on such a low level as C++. –  Kenny Feb 28 '13 at 16:03
5  
Why this post gets 5+ ??? It does a) not answer the question b) isn't correct –  hek2mgl Feb 28 '13 at 16:15
1  
So references are slow and consume too much memory? –  Rocket Hazmat Feb 28 '13 at 16:22
1  
@RocketHazmat Yes, strangely enough that's true. –  Yannis Rizos Mar 2 '13 at 13:27

PHP already does a copy-on-write thing where it doesn't create a new value til you change something, so there's not much memory saved by using references. Doing so can even mess with some stuff PHP does internally to reduce memory usage, making things even worse.

Add to that the fact that references make things a bit too magical in general. The default, and thus what most people expect, is pass-by-value; when i pass $i to a function, it complicates things tremendously to have to care whether that function mysteriously changes $i to something else entirely, and thus make defensive copies just in case. (It can already modify $i if the value is an object, but in my opinion it shouldn't.)

Basically, i'd only find pass-by-reference useful for "out" parameters, meaning variables i expect to get back from the function rather than pass in, a la preg_match's &$matches. Even for functions that clearly modify the object being passed in, like sort or array_pop, that feels a bit icky...but that's what we're stuck with.

share|improve this answer

Some reasons on the fly :

  • PHP is a scripting language, and doesn't aim to be used as a core embedded software (basically, memory is cleared at the end of the script),
  • Since PHP5, pass-by-reference is implicit for object parameters (so PHP uses pass-by-reference "in your back").
share|improve this answer
1  
See: php.net/manual/en/language.oop5.references.php for your second point. –  Yannis Rizos Feb 28 '13 at 17:02

I think this is just a choice of the developers, nothing to do with the language. Plenty of PHP code (built-in and not) use references. Take a look at the array functions in PHP, lots of those use references. preg_match uses references.

I think one of the reasons developers choose not to use references is because it can be confusing. You call a function and one of the variables may (or may not) be updated because it was a reference. So, when you debug, it may not be clear why the value of $x jsut magically changed.

share|improve this answer

The fundamental problem with your question is that you assume that this is common in C++, too. It isn't. We don't like output parameters and use them as little as possible- they're only really common in C-style APIs.

share|improve this answer

I know many php functions that do so. Have a look at preg_match() for instance. $matches will be passed by reference

If you want write a function for yourself that takes arguments by reference then use the following syntax

function byref(&$a, &$b, $c) {
    $a += $c;
    $b += $c;
    return $a * $b;
}

$a and $b are passed by reference $c is passed by value.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.