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?
5
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:
- PHP references explained
- Objects and references
- In PHP (>= 5.0), is passing by reference faster?
9
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.
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.
4
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”).
1
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.
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.
0
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.