Becoming PHP6 Compatible
Hey there! Welcome to Making the Web - my personal blog about website development. Feel free to subscribe to my RSS feed to keep up with the latest. Alternatively, subscribe by email. Hope you enjoy this article!
PHP developers, like all developers, want their scripts to be as compatible as possible. Often, this involves looking into the past, to see if scripts are backwards-compatible. But, we sometimes have to look into the future - to see what is changing, and to understand what we need to do to become compatible.
PHP6 is the latest, yet unreleased version of PHP. It is still under development and won't be released for some time yet. Nonetheless, it is still important that we consider the changes we know about at the moment, and write scripts which are compatible.
If you want to make use of PHP6 when it comes, you're going to have to write your new scripts so they are compatible, and possibly change some of your existing scripts. To start making your scripts PHP6 compatible, I've compiled a list of tips to follow when scripting:
Don't use register_globals
In PHP6, support for register_globals will be no more. There will be no option to turn it on or off - it will not exist. This change should not affect you, as you shouldn't really use register_globals anyway. If you don't already know, register_globals puts $_REQUEST into the global scope, so you can access the variables just like any other variable. Instead, you should access inputted data like this:
$_GET['input'];
$_POST['input'];
$_REQUEST['input'];
Stop using magic_quotes
In my opinion, this tip should be applied whether you are using PHP 3, 4 or 5. Thankfully, in PHP6, the magic_quotes feature is going to disappear with register_globals. For those who don't know, magic_quotes automatically escapes single quotes, double quotes, backslashes, and NULL characters.
Don't Register Long Arrays
If you access user inputted data using $HTTP_POST_VARS or $HTTP_GET_VARS, you better stop now. Instead, you should use the superglobals - $_SERVER, $_COOKIE, $_GET, $_POST, $_FILES…
preg instead or ereg
If you're using ereg functions for regular expression tasks, then you should start using the preg functions instead. ereg will not be available in the PHP core as of version 6.
Don't initiate objects with the reference operator
If you're initiating objects using the reference operator, you should stop now. It will generate an E_STRICT error in PHP6.
$a = & new object(); // Do not do this;
$a = new object(); // Do this as of PHP6
For more changes in PHP6, read Unofficial PHP6 Changelog.
Tags: compatibility, objects, PHP, php6, register_globals












Günün Bağlantısı: PHP 6 ile uyumlu olmak Said,
September 23, 2007 @ 3:01 pm
[…] için http://bitfilm.net/?p=18 bağlantısına bakabilirsiniz […]
Paul Said,
September 24, 2007 @ 1:49 am
I would argue that 1-3 shouldn't even be an issue, regardless of whether you're writing PHP4/PHP5/PHP6 compatible code. Those three rules are basic PHP requirements now. The fact that there is even a register_globals variable is a hint at the problems that PHP should never have had, and they are scars that we should set properly now and ignore.
#4 & #5 are general "good idea" practices that should also be adhered to. preg_replace() is just a tad faster than ereg_replace() (although it's minimal), and unless you're on a LAMP server with 128MB of RAM, use of references is generally unnecessary.
Just my 1.98374967239479867324 cents.
All in a days work… Said,
September 24, 2007 @ 2:26 am
[…] Becoming PHP 6 Compatible Don't use register_globals. Stop using magic_quotes. Don't Register Long Arrays. Use preg instead or ereg. Don't initiate objects with the reference operator. (tags: PHP) […]
dgurba Said,
September 24, 2007 @ 2:27 am
Actually I disagree with (1) given that php5 released filter(). We should never use the superglobals directly … thats just bad (common) practice.
Use Filter() [php5 only]:
http://us3.php.net/manual/en/ref.filter.php
Or, Inspekt [php4/5 compatible]:
http://code.google.com/p/inspekt/
-dg
Adam Said,
September 24, 2007 @ 3:54 pm
I'm not trying to be obnoxious but in points 4 and 5 you should replace "your" with "you're" as in "you are". "Your" implies possession. I apologise for being a grammar nazi; it's just irritating to read.
It looks like I'm already PHP6 compatible! Woohoo! I should probably go and check out what new features are promised
she Said,
September 24, 2007 @ 6:00 pm
Frankly Adam, I dont think many people care. It may be irritating to read, but its just one little grammar mistake. The world can survive that …
Michael Morgan Said,
September 24, 2007 @ 7:37 pm
Woooh - My code should survive the switch fine then.
But I really can't see PHP 6 taking off anywhere as fast as it's predecessors have. PHP 4 gained huge popularity and mass usage worldwide extremely fast however the change to PHP5 hasn't followed suit. PHP 5 is struggling as a lot of hosts (specifically shared hosts where it has the largest impact) simply wont upgrade because of the problems it can cause.
Therefore I don't think it's really going to matter if your code is PHP 6 compatible.
Max Design - standards based web design, development and training » Some links for light reading (25/9/07) Said,
September 24, 2007 @ 10:51 pm
[…] Becoming PHP 6 Compatible […]
現在寫 PHP6-compatible 的一些技巧 at Gea-Suan Lin’s BLOG Said,
September 25, 2007 @ 12:04 am
[…] Becoming PHP 6 Compatible 這篇裡面提到的五個技巧,現在遵循這些習慣,等到 PHP6 正式推出的時候程式碼才不會改太多: […]
Montoya Said,
September 25, 2007 @ 1:51 am
No more ereg? Darn! Those are so much easier to write than preg!
沒力小僧,羞道人的糟糕日誌 » PHP6 compatible Said,
September 25, 2007 @ 3:38 am
[…] 在 DK 大那邊看到的文章,原文來自 Becoming PHP 6 Compatible 。 […]
SiteVoter Link Community Said,
October 2, 2007 @ 3:09 pm
Becoming PHP 6 Compatible
At the end of the year it's do far, the active development of php4 will end! While php4 scripts are still work in php5 this is not possible anymore in php6, classes written for php4 need to get updated to make them work. But there is more check this a…
PHPRomania Blog » Blog Archive » Compatibil cu PHP 6 Said,
November 30, 2007 @ 5:10 pm
[…] - Nu folositi register_globals. In PHP6 nu va mai exista nici o optiune pe care sa o puteti accesa.Veti putea accesa variabilele folosind: $_GET['input']; $_POST['input']; $_REQUEST['input']; - Nu folositi magic_quotes. La fel ca si register_globals, acesta va disparea. - preg in loc de ereg. Daca folositi functia ereg, va trebui sa incepeti sa folositi preg, deoarece nu va mai exista in core-ul PHP incepand cu versiunea 6. - nu instantiati obiecte folosind operatorul referinta. Va genera un mesaj de eroare E_STRICT. $a = & new object(); // Nu $a = new object(); // Da Vezi tot articolul aici […]
PHPDeveloper.org Said,
December 5, 2007 @ 2:11 pm
Making the Web Blog: Becoming PHP 6 Compatible
On the Making the Web blog, there's this post that talks …
developercast.com » Making the Web Blog: Becoming PHP 6 Compatible Said,
December 5, 2007 @ 5:35 pm
[…] the Making the Web blog, there's this post that talks about looking forward with your code and making it ready for when PHP6 comes around. […]
Lothar Obfuscator Said,
December 5, 2007 @ 9:22 pm
@she… Actually, I'm fully with Adam on this. It is such a simple thing to do, why not do it the right way? Poor grammar jumps off the screen and shouts "sloppy" (or worse, ignorant).
You go Adam! By the way, may I recommend a great book that relates to this issue: "Eats, Shoots and Leaves: The Zero Tolerance Approach to Punctuation."
onPHP5.com Said,
December 6, 2007 @ 9:09 am
Funny everyone forgot that curly braces {} cannot be used as string offsets in PHP6.
Matt Said,
December 6, 2007 @ 6:50 pm
Any source on the curly brace removal? I haven't seen that mentioned anywhere.
Brendon Said,
December 6, 2007 @ 6:54 pm
@Matt http://us.php.net/manual/en/language.types.string.php says "… the {braces} style is deprecated as of PHP 6"
Matt Said,
December 6, 2007 @ 9:58 pm
Thanks. So it's only the offsets.
// Alternative method using {} is deprecated as of PHP 6
$third = $str{2};
// However, this is still legal in PHP 6 correct?
$third = "Hello {$str}";
Brendon Said,
December 6, 2007 @ 10:34 pm
I guess that it's legal, and I can't find any sources to suggest otherwise.
Mike A. Said,
December 10, 2007 @ 8:08 pm
wow, i was just taught to use magic_quotes in a class i recently took. weird finding out that it's bad practice. anyway, what are you supposed to use in lieu of magic_quotes? clean it manually with a search and replace?
Brendon Said,
December 10, 2007 @ 10:18 pm
@Mike A. If your using input in database queries, use mysql_real_escape_string(). You may also want to look up addslashes().
Мастерская интернет-разработчика » Пора делать свой код совместимым с PHP 6 Said,
December 14, 2007 @ 11:56 am
[…] блога "Making the web" предлагают уже сейчас писать код с учётом требований […]
Kumar Chetan Sharma Said,
December 15, 2007 @ 10:10 am
I have been using PHP since we used to have extension "php3", PHP4 was wow and PHP5 is totally OOPSed. But one thing, I still don't see wide support for PHP5 and first 3 points were recommended when I moved to PHP4. I have not seen PHP5 on any shared hosting server. This is a problem. Majority of the PHP5 code I have written is either totally custom or is lying on my Ubuntu machine. Will PHP6 do the magic?
Блогинг как стиль жизни » Blog Archive » Пора делать свой код совместимым с PHP 6 Said,
January 2, 2008 @ 11:02 pm
[…] блога "Making the web" предлагают уже сейчас писать код с учётом требований […]
RAM Said,
January 10, 2008 @ 12:02 pm
There is a very simple solution to all of this. It is called programming in almost pure PHP 3.0.18.
Don't think it can be done? take a look at http://freeprogs.us.tt/?page=phpcompat
If you love structured programming (the good old days), this shouldn't be hard to do. If your a zealot for overcomplicated OOP then this is impossible, so just keep on rewriting your PHP programs with each new milestone, or switch to another language that doesn't change as often, like a compiled language (CGI) or Perl.
RAM Said,
January 10, 2008 @ 10:44 pm
Montoya,
"ereg will not be available in the PHP core as of version 6"
The key word is "core"
I suspect ereg will finaly be turned into an extension, like pcre has been since PHP3.
Hopefully it will be enabled by default in php.ini (pcre in PHP3 wasn't.)
Normal Said,
January 12, 2008 @ 6:36 pm
@she
Frankly, she, you're wrong.
Your abuse of grammar upsets me further; you should have used "don't", not "dont", and "it's", not "its".
Your lack of concern is evidently rather ill-judged.
RAM Said,
January 13, 2008 @ 11:37 pm
For a comprehensive list of things that will change see the following URL:
http://php.net/~derick/meeting-notes.html
It is somewhat technical, and very detailed
5 CONSELHOS PARA CRIAR CÓDIGO COMPATÍVEL COM PHP6 » Pinceladas da Web - Reflexões sobre XHTML, CSS, PHP e WebStandards Said,
January 14, 2008 @ 8:05 am
[…] Making the web - Becoming PHP 6 Compatible. […]
Dwayne Charrington Said,
February 1, 2008 @ 1:58 am
How typical. I come onto yet another blog full of people correcting grammar mistakes. Yes bad grammar is unprofessional, but seriously why do people go to these extreme measures to correct people on it?
Normal, I would love to see your degree's in literature or any other English degree's you have obtained.
Seriously, if grammar concerns you so much to the point of correcting someone who spelt it's 'its', why don't you become a school teacher or something?
Why even bother posting in the first place, if you're only going to pretend to know what you are talking about?
All of my PHP scripts are already ready for PHP 6 it would appear. So I guess I am all good.
- Dwayne Charrington,
http://www.dwaynecharrington.com
jackrabbit slim Said,
March 15, 2008 @ 2:15 am
@dwayne
Anyone reading this page is here because they are programming in php. I come from a generation of programmers who take pride in their work, this means writing code which is not just efficient, but easy to read.
A life of coding in various languages, learning correct syntaxes and coding in accepted styles invariably leads to an obsession with language.
This obsession leads one to produce coding algorithms which are as beautiful as they are functional, resulting in a code purity which is as important to the writer as it is to any other coder who may have need to understand the algorithm in a few years time.
The obsession extends to any form of communication requiring information exchange through the use of language - this includes email communications and blogging.
As someone who is actively involved in the hiring of programming staff, I can assure you that use of correct grammar in job applications is essential.
If you cannot express your ideas according to the rules of your native language what hope have you got of expressing your programming solutions succinctly and correctly in a language you learned just a couple of years ago?
So I would go further than Adam, Lothar and Normal: it is of paramount importance that the grammar in blog posts which are directed at programmers is error free. Incorrect communication implies at best laziness, at worst ignorance - either way the integrity of the blog is fundamentally undermined.
Finally, you do not need degree's [sic] to utilise the English language correctly, a simple pride in your output should suffice.
Lew Payne Said,
April 2, 2008 @ 6:12 am
Well said, jackrabbit slim. I share your obsession with both code and language in general. Unfortunately, Dwayne Charrington fails to comprehend the difference between a typographical error (i.e., a mistake) and illiteracy (e.g., poor education). I've often wondered how people who take their illiteracy for granted, and see no reason for concise communications, would do in numerical programming. I can hear it now… "so what, I'm a few digits off. Big deal!"
I too am from the original PHP days, when there was only PHP/FI and an active mailing list with about 30 of us on it. I too take programming seriously, as an art form. Most instant self-made coderes these days take it as a means to an end, and exhibit little pride in their craftsmanship of same. What a shame that is. But then again, when someone needs a bullet-proof application, that's why there are folks like us out there.
Regards,
Lew Payne
John Said,
April 11, 2008 @ 1:52 pm
addslashes and stipslashes wont be available in php6 either
Kev Said,
April 16, 2008 @ 2:06 pm
Oh bother!
Why are blog comments everywhere spammed with such pedantic wastes of time and web space by typically over-educated, over-inflated egos displaying their ignorance of the article writer's intended point of focus?
C'mon! You're even obscuring the focus for the readers!
Children, this class is PHP Programming 101 … Most of us arrived here because we hoped to obtain useful information from an article posted under a topic of particular interest to us. We (I at least) prefer NOT to have to scroll through so much irrelevant spam. English Grammar is in the next building, kiddies!
Regarding the author and his article:
"Mate, ya article is a bloody ripper! I've a bit of a slog ahead of me ta be PHP 6 compliant but thnx to ya info I'm on me way. Bewdy mate!"
[Can the pedantic time wasting egos resist criticizing the lines in quotes?]
Kev.
[Self-Taught Programmer (or QUOTE: Lew Payne "instant self-made coderes" Tsk! Tsk!) and Self-Declared Gumby whose code almost/nearly/always passes Quality Assurance Standards and Compliancing after several pre-release revisions …and mega-liters of coca-cola/caffeine.)
SEO Said,
May 23, 2008 @ 11:21 am
Very good article! I am looking forward to trying php 6 and all its power features!
Lew Payne Said,
June 27, 2008 @ 12:03 am
"Why are blog comments everywhere spammed with such pedantic wastes of time and web space by typically over-educated, over-inflated egos displaying their ignorance of the article writer's intended point of focus?" - Kev
I typically find blog comments spammed with illiteracy, making them difficult to read as well as obscuring the author's intended meaning. I don't consider knowledge of basic grammar and spelling rules, as taught at an elementary school level, to be over-education. Nor the expectation that grown adults operate at an elementary school level or above to be unreasonable.
In case you're confused, my comments are in reference to illiteracy and not the occasional typographical error which we all make.
JavaM » Blog Arşivi » PHP 6 ile uyumlu olmak Said,
August 5, 2008 @ 9:12 am
[…] için http://bitfilm.net/?p=18 bağlantısına bakabilirsiniz […]