Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« Zend PHP Framework not a rumor
Harry Fuecks, PHP Patterns and Dynamically Typed »

Of PHP References and Compatibility

October 20th, 2005

Following up on the Backward compatibilty and web host adoption of PHP 5 post from two weeks ago, I got a pretty good education about the problem with taking a reference of $this. The best summary is in this bug report.

It comes down to the fact that allowing references to be taken of $this can lead to unexpected values for $this later on. ($this re-assigned) However, it does not appear that a good solution exists:

Given the choice between: (A) doing nothing but leaving room for confusion, (B) taking away the ability to create references, or (C) checking assignments for references to $this(read: slow); The current decision is to go with (A).

The cause of the problems were a result of diverging away from strategy A and a little bit into strategy B. I’m still working on compiling 5.1rc3, so i’m not yet sure how this was actually resolved. I wouldn’t feel bad about a warning when this occurred. However, I think a fatal error is absolutely wrong here. I do feel confident this will get resolved in an acceptable manner because I’m fairly sure that unless it becomes illegal to pass $this to a function by reference that there will always be a workaround for making code compatible with PHP 4 regarding $this. Thanks to everyone who looked in to it.

In the meantime, Derrick Rethans made an article he wrote for php|architect explaining PHP References available. You should read this article. I had to print it to follow it, but I learned something that I did not know. I did not know that PHP uses a copy on write semantic for normal variables. The one question that I wish the article had covered was how objects get automatically passed by reference in PHP 5.

The article debunks some myths about using references for performance. I experimented with this a bit when PHP 4 first came out, but quickly gave up on using references for optimization. I ended up with too many mysterious bugs to track down from “magically connected” variables. I finally ended with some fairly limited rules for using references. I avoid them in all cases except when passing values to functions by reference and for variables that held objects, in which case I use them always. When I want mystery i’ll go to the library, thank you. I don’t need it in my software.

I’ve been looking at the changes necessary for the php 4.4/5.0.5 update. I wrote a little scanner program to help locate suspicious return statements. (I’m far from E_STRICT anyway.) But, I have no programatic help for finding “Only variables can be passed by reference” errors except to actually trigger the error.

I’ve been a little bit frustrated about getting over the compatibility hump with 4.4/5.0.5/5.1rc3. But that seems like nothing compared to the ingrate who emailed Derrick fan mail. This dude needs to get a grip.

I don’t think the update was a mistake at all. Like I said earlier, I’ll take my PHP without mysterious bugs, please. Rather, I think the mistake here was in not understanding the backward compatibility issues. I went back and read some of the orginal discussion about this update. What strikes me was that binary compatibility was discussed, but no-one seemed that excited about the backward compatibility issues at the code level.

So a mistake was made. These things happen. Steps have been taken to ensure that something like this is less likely happen again. Derrick’s fan needs to get over it and move on.

Along these lines, It seems like with so many projects using automated testing that there should be a process for projects with automated test suites to package them up and donate them to an automated backward compatibility lab.

categories PHP
tags performance-optimization, php-5, php-references

Related Posts

  • PHP 5.1 is out
  • Backward compatibilty and web host adoption of PHP 5
  • PHP 5 adoption
  • Let Your Properties be Properties
You can leave a response, or trackback from your own site.

4 Responses to “Of PHP References and Compatibility”

  1. #1 Wez Furlong responds...
    October 21st, 2005 at 1:13 am

    Objects are not actually passed by reference in PHP 5. In PHP 5, objects are represented by object handles (a bit like resources) and those handles are passed by value to functions and methods. The engine knows how to de-reference those object handles to refer to the actual object instance. This mechanism is totally separate from the pass-by-ref mechanism.

  2. Jason E. Sweat’s weblog » Advise for an Aspiring PHP Developer pingbacked on November 2nd, 2005 at 6:27 am
  3. PHP 5.1 is out | Professional PHP pingbacked on November 25th, 2005 at 1:02 pm
  4. #4 Sid responds...
    December 23rd, 2007 at 4:01 pm

    Well, php5 objects arent passed by reference as per php 4 definition but they are passed by reference like in Java.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Add to Google
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (95)
    • Software Design (28)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  94
      noujoum, Michiel Van Kets, isorabins [...]
    • Sarah Snow Stever  24
      Dubai Web Design, Development, Snowcore, ennah [...]
    • Benchmarking PHP's Magic Methods  8
      stas, minikperi, Shelon Padmore [...]
    • Keywords and Language Simplicity  7
      olmse, Handy, minikperi [...]
    • Even the Big Guys Get Validation Wrong  4
      James Benson, Michael, Enric Naval [...]
    • Improved Error Messages in PHP 5  12
      James Benson, Iron, baggreeddog [...]
    • How to Transfer Mac OS X Application Data between Computers  36
      Secret Santa, micala, Khaled bin Alwaleed [...]
    • Programming Language Trends via Google  15
      MattW, Jeff Davis, retry [...]
    • PHP Development From Java Architects Eye  10
      Grrkkvho, Bobrila, FelhoBacsi [...]
    • The value of MVC  9
      Euvmetkk, Bobrila, Vulchak [...]
    • nofollow and comment spam  5
      Scopmazo, Qmumqckx, sss [...]
  • Pages

    • Tags
  • Recent Posts

    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
  • Archives

    • 2008: May
    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Log in