Writing for Future PHP6 Compatibility

by Mike Willbanks on December 3rd, 2005

With the PHP 6 Developers Meeting Notes out I finally took a while to go and read through them and figure out what is the best way to move forward and start writing code that will be compatible not only for 4x and 5x but continue on though 6x. While sometimes it is not possible to keep everything being deprecated if you are using 4 or 5.0 there are some recommendations that you might want to follow.

Register Globals

If you are currently using them STOP! Register Globals is not a very good thing to have turned on by default anyway, most developers I have seen do not understand either the impact of them being on or how to combat them. These will be removed in PHP 6 anyhow.

Magic Quotes

Magic quotes are going to be removed from PHP6. So if you are using them you might want to try and code against them. A good way you might want to check against them is by the following:

if (version_compare(phpversion(), '6', '>=')) {
function get_magic_quotes_gpc(){
return false;
}
}
if (get_magic_quotes_gpc()) {
stripslashes(); //your input
}

Call Time Pass by Reference

Call time pass by reference is going to be removed… So all of you that are currently initiating your objects with the reference operator should stop doing it, although it will be an E_STRICT error, you might still want to only use it if you must!

$var =& new stdObject(); //current object reference
$var = new stdObject(); //just do this instead :)

zend.ze1_compatibility_mode

Do not run in this mode. It will give a core error and halt execution if you use it in PHP6. If your PHP4 applications do not run without that setting on, fix it to run with it off and continue on.

Long Globals (HTTP_*_VARS)

These will be removed and if you are not currently using $_POST and $_GET why not?!

Case Sensitivity

They are starting to move away from the case insensitivity of functions and classes. Try to use all lowercase, also it makes it much nicer to know if you are using a custom function that way!

Dynamic breaks

They are removing support for dynamic breaks. So for those of you who do: break $myvar; Find another way and clean up that messy code!

Database Extensions/Objects

Move to PDO as soon as you can (PHP 5.1). The plan is to slowly move Non-PDO database extensions out of the core. It would just be best to migrate to those as soon as you can.

ereg

If you use ereg for your regular expressions move to preg since it is faster and also ereg will be moved out of the PHP core.

Strings and {}

If you are currently accessing the letters of a string by $str{1} you should move to []. Yes the manual told you to use {} but now they are going against that because people want to use []. So they are going to throw away {} in PHP6 while it is deprecated in 5.1.

@ini_set

Since the @ operator is currently very slow they are removing support for it to work on ini_set. Might want to check to see if you can use ini_set for certain operations before trying it!

Open and Closing Tags

The and also < % %> will be removed. Also < ? is starting to be looked down upon. Might just want to start going with for everything.

Conclusions

That is about all I thought was worth mentioning when talking about future compatibility. If you are still using php4 to php5 compatibility, you might want to think about removing PHP4 in the coming months since there is already 5.0 and 5.1 versions out. Yes I know you guys do not like to give up your old ways but it is time to move on.

What I actually thought about doing was writing versions strictly for php5.1 or 6.0 and then having a cvs to tag and branch them out. So I actually have versions for 4.3, 4.4, 5.0, 5.1 and then a head revision for whatever comes next. Just a few thoughts on how to manage it.

From PHP

6 Comments
  1. Jens Kleikamp permalink

    Hi Mike,

    that´s a great list, thanks a lot!

  2. toto permalink

    PHP is sleeping…. I understand that the core is changing a lot, but com’on… on the dev. side they only introduce incompatibility issues…

    There is not big step forward…. Still stucked with a semi-OO language…

  3. PHP is not sleeping at all. Look at PEAR look at PECL. These things are flying.
    There is a lot of reasons for why PHP took the approach it did on OOP. That is something many people do not understand. Why would someone want to make PHP all OOP to even function? Not only would that be wasting performance but also creating more incompatibility as you would call it.

    Most of these changes are much needed changes actually and should be very important to developers. The list I put up was only of the changes that would effect the developers not the new features. Unicode will be fully implemented which is much needed definitely if you are developing internationally. There are many many more.. Take a look at the meeting notes:
    http://www.php.net/~derick/meeting-notes.html

    And also the PHP6 blog:
    http://php6dev.blogspot.com/

  4. gregf permalink

    Great list. Good to see the urge for people to move to pdo.

  5. “And also the PHP6 blog:
    http://php6dev.blogspot.com/

    It is not the PHP6 blog, it is the blog of someone having copied the contents of our wiki in a blogspot site :)

    There is no blog for php6, the best way to know what’s going on is to follow internals or read planet-php :)

  6. I certainly agree Pierre. It was just a great resource at the time that I had put this post together before it was all simply copied. There used to be additional comments there and now it is simply a copy of the internals posting.

    Internals recaps that are posted on the Weekly Summaries at Zend also are a great help if you don’t like following all of the posts on internals.

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS