Why are references rarely used in PHP?

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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật