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.

Filed Under

  • PHP

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. Wez Furlong says:
    10/21/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 says:
    11/2/2005 at 6:27 am

    [...] Some good blogs: http://www.procata.com/blog/feed/rss2/ http://www.lastcraft.com/blog/wp-rss2.php http://netevil.org/feeds/rss.xml and many others you can find through http://www.planet-php.net/. [...]

  3. PHP 5.1 is out | Professional PHP says:
    11/25/2005 at 1:02 pm

    [...] After the Reference compatibility thing I thought there was supposed to be a process in place to catch surprises like this. What happened? [...]

  4. Sid says:
    12/23/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

Click here to cancel 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

    Subscribe Feed
    Share Subscribe to this blog…
    Share Bookmark or share this page…
  • About

    My name is Jeff Moore. I'm a PHP programmer living in San Francico and working for a startup.

    More about me…

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (17)
    • Open Source (14)
    • PHP (98)
    • Software Design (29)
    • Usability (14)
    • Web Design (20)
  • Recent Comments

    • Programming Language Trends via Google  19
      Craigslist pva, jessica, Scott [...]
    • Looking Towards the Cloud  35
      bentonville multiple listing, cosmetic dental, Sam Brodish [...]
    • PHP versus ASP  8
      Marhta Blight, Ravi, Ryan Brooks [...]
    • How to Transfer Mac OS X Application Data between Computers  59
      Website Migration, harry the computer support guy, Dotty Salvage [...]
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  157
      lehuuphuc, Robert Parthemer, Lingerie Intimate [...]
    • PHP Games  25
      jessica, Tennille Cranor at Chilli Plants, Lucas Ortell [...]
    • un-PEAR-ing  5
      jessica, Eugene Panin, Arnaud [...]
    • The Legality of Republishing RSS Feeds  23
      kevinxiao, Marissa Miscovich, Quick Student Loans [...]
    • Faster Page Loading  4
      jessica, angular cheilitis, Aaron Rosenfeld [...]
    • PDO versus MDB2  15
      jessica, kevinxiao, Gavin [...]
  • Recent Posts

    • ZendCon: Writing Maintainable PHP Code
    • Looking Towards the Cloud
    • Holiday Tech Support
    • Closures are coming to PHP
    • 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)
  • Site

    • Archives
    • Log in
  • Search