References to the Max

It’s somehow hard to understand. PHP Variables and how they do References to the according data. This Blog post is a collection of articles related to the topic and a short review of those I found valuable. Maybe there are more important ones, please let me know via the comments.

The story behind this post is, that within the WordPress project we need to currently still keep PHP 4.3 backwards compability. One topic that is related to the differences between the outdated 4.3 version and the current 5.2 version is the handling of values for object variables.

I saw some issues within the core-code and I was asking in some tickets (#11663, #11780) about how to handle them. It was important to me to get feedback from core developers because in other situations when it came to references in a patch nothing concrete was available and specs were missing. That lead to the situation that certain myth and wrong information is circulating. At least partly wrong information. I do not like that and I would love to see this clarified.

I thought: Well even if we’re currently wrong with the topic, it would be good to have a spec or similar at least to referer to while trying to figure things out. And while discussing those in IRC yesterday, we came to the point that it’s often a question how a value is to be used then the type only. For example in PHP 5 passing or returning references can be counter-productive where in some cases in PHP 4 those are a need to get certain functions in wordpress to work.

Functions returning cache objects were named as examples where that is the case. Otherwise those objects would get copied on change and the changes won’t be reflected overall in memory and/or within the database or on disk occasionally. And the cache should be something where it’s possible to write into.

Another thing is the $this variable. By PHP Documentation that one is “by reference” automatically so there never is need to prefix it with a &-sign. But within the core-code this is done all the time when it is passed it into the hook system. Myth or reality? At least that’s the documented behavior.

But those are some aspects of the problems within the wordpress project only. In general the topic is not easy to understand and I thought it’s time to collect some valuable resources. On the way to do so, I revealed some pretty interesting texts incl. one that is mathematically proving why the topic is hard to get. Because the implementation in PHP comes with it’s flaws.

PHP Reference related articles

I collected about five resources I found worth to note. Those are mostly from PHP core developers sharing the information they have with the reader. They range from highly theoretical works to practical guides so it’s possible to get a broad view on the topic. Here we go:

Copies versus References

In her Zend Developer Zone Article about writing PHP Extensions in C, Sara Golemon explains the concept of copy-on-write referencing and full referencing as the two forms of references that are available in PHP. This is pretty well explained with PHP code and the internals, she draws a nice picture. The whole ZVAL section is pretty well, you get the details explained with PHP and C code side by side. The right choice for the technical insight and the internals upfront.

References in PHP: An In-Depth Look

Derick Rethans explains in his article (originally published in PHP Architect June 2005) how data is associated to variables and what this implies. That covers the ZVAL container and the internals associated with it like the symbol table, reference counting and assigning by reference from the PHP internals. Multiple images and PHP code snippets help to understand the issues. No wonder it covers pass and return by reference as well for 4.3.0 and above. If you need examples and images, you should read this article, it’s a well written article for a PHP developer.

Do not use PHP references

On his Blog, Johannes Schlüter recently took the opportunity to put a resumé of discussions online he had while doing speeches on conferences and getting into contact with other developers. So his article is backed up with practical experiences from other developers which makes it extremely well readable. He offers some best-practice-tips and provides examples. A nice read on the topic you should not miss – incl. a provoking title.

Copy-on-Write in the PHP Language

How to deal with the copy on write paradigm in your daily programming? Is there a flaw or are you just making errors? Highly theoretical paper and work done by four japanese scientists. Be aware that you need some math and engineering skills as well as scientific ones to be able to read this document. Akihiko Tozawa,  Michiaki Tatsubori, Tamiya Onodera form IBM Research Tokyo and Yasuhiko Minamide from Department of Computer Science, University of Tsukuba are providing a kind of proof about References semantics and behaviour we as PHP developers are facing day by day in our work. When you’re ready with this document you should be able to play on the topic with ease.

The PHP Manual

Last but not least the PHP Manual itself has multiple places regarding references and reference count. The section References Explained is probably the best known one. It contains what references Are (Not) / Do as well as Passing and Returning and unsetting / spotting those. But that’s not all, inside the Garbage Collection section there is an interesting article about Reference Counting Basics.

Conclusion

Hopefully you find these resources helpfull. The topic is not easy to understand per se and each time I think I now know it I experience something new in the field. So creating this list did help me and I hope it’s the same for you. I think this article should provide enough background information for the discussions to come. If you don’t think so, just let me know what you think is missing.

Links

About these ads
This entry was posted in Hacking The Core, Hakre's Tips and tagged , , , , , , , , , , , , , , , , , , , , , , , . Bookmark the permalink.

2 Responses to References to the Max

  1. Pingback: WordPress 3.0 and PHP 4 – What’s the deal? | hakre on wordpress

  2. Pingback: WordPress drops PHP 4 in Q1 2011 | hakre on wordpress

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s