Don't think that using references will lower your RAM usage

Objects are always copied by reference in PHP 5, which means that copying even the most complex object is instantaneous and essentially free in terms of memory usage. Furthermore, PHP makes extensive use of "copy-on-write", which means taking ten copies of the same array is also free, because they all point to the same object. It's called "copy-on-write" because the minute one of the arrays change from the others, a full copy is made so the change is kept separate.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Be wary of garbage collection, part 1 >>

Previous chapter: Pre-increment and post-increment aren't the same

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.