Recently, I’ve been putting a lot of time and effort into leveling up my PHP skills.
When my last round of leveling up began, I knew a good bit of basic-to-advanced PHP. The thing was, I had come to the realization that I simply felt comfortable with my level of proficiency.
Once I realized this, I immediately wanted to get uncomfortable again, in a manner of speaking. I had a (stronger than usual) drive to get lost in the weeds of PHP obscurity and come back on the other side writing sleeker, leaner, more maintainable code.
Namespaces
The core of what I embarked to learn were namespaces and autoloading. This led to a better (albeit basic) understanding of many other things I didn’t expect to encounter, like using Composer to install third-party PHP libraries.
While all of this is fascinating and definitely blog-worthy material, for the sake of simplicity let’s just say that in a nutshell, namespaces are the basic concept that I knew nothing about and really wanted to learn inside and out.
It’s also worth mentioning (and essentially the point of this post) that I was well-aware of the fact that using PHP namespaces is a problem in older versions of PHP.
In fact, if you use namespaces in your PHP code and your PHP version is outdated, the world will completely end. In truth, your site will just crash and you’ll be left with a white screen and (if you’re lucky) and error message.
About PHP Versions
In a perfect world, the oldest PHP version that any site would currently be using is PHP 5.6 (at least at the time of this post). Not only is this my personal opinion, but according to PHP’s official site:
…you are strongly urged to upgrade to a current version [PHP 5.6 or above], as using older versions may expose you to security vulnerabilities and bugs that have been fixed in more recent versions of PHP.
In fact, I believe that for many sites, making the leap to PHP 7 is well worth considering and not nearly as scary as it might seem. Of course, this will all depend on the site in question and its hosting environment.
Unfortunately, for some hosts (like one I recently had an experience with), updating PHP is simply not an option, even if the version they run is out of date.
Get Pro-Active
If you own a website and you’re unsure what your PHP version is (or even what this means), call your host or open up a support ticket and ask them! The biggest question is, “Is my PHP Version 5.6 or higher?” If they say “No,” and if they also give no indication that they plan to upgrade, then it’s time to switch hosts!
In my experience, most hosts are active in responding to support tickets and will gladly update your PHP version if you just ask them to. Note that this might cause adverse results if you are using very old software on your site, but most hosts will keep an eye on that and undo the change if something goes wrong.
If you are running WordPress and not using abandoned plugins, the chances are you will be fine on PHP 5.6 or even PHP 7.
About WordPress
There has been a lot of discussion in the WordPress community about PHP versions, and for good reason.
Without digging too much into the controversy, the pertinent point is that WordPress avoids doing anything in its code base that would break any site using outdated PHP versions down to version 5.2 (note PHP 5.2 was pronounced dead in 2011).
The relevant point for me is that I learned a lot of what I know about PHP by working on WordPress sites. And since this environment is not one where you’ll likely find things like PHP namespaces, this may have contributed to me not having learned them yet.
Since WordPress core (and most popular plugins in the repository) respect the fact that millions of sites would break if they did something like use PHP namespaces in their code, they just simply don’t use PHP namespaces in their code.
The discussions about PHP versions in the WordPress community helped me to think about and understand 4 key things:
- A great many site owners don’t know and/or don’t care about their PHP Version
- Most hosting companies understandably don’t want to force mass PHP updates that could potentially break thousands or millions of sites
- This combination probably leads to millions of sites that are not going to have modern PHP versions for quite some time
- When I use namespaces in my PHP code within a WordPress project, I better be careful which servers I upload my code to
My Experience
I began this post by talking about my drive to level up my PHP skills. And then I took a 90 degree turn into PHP versions, hosting, and WordPress. So where am I going? What’s the point?
The point is that PHP versions affect many things, from security to speed, all the way down to the types of code developers can write without breaking sites.
When I became confident enough in my usage of namespaces to use them in an actual project, I was stoked. Namespaces and autoloading had totally redefined the file and folder structure of my project, and I was loving it!
….until I wasn’t.
When I tried to upload my project to a test site on WP Engine (a very prominent and respected WordPress host), I promptly got a fatal error. After digging around, I discovered that WP Engine uses PHP 5.5 (outdated!) for all sites on their platform and would not let me upgrade to PHP 5.6.
I was feeling things on many different levels:
- Upset at myself for not realizing that every WP Engine site I’ve worked on uses an outdated PHP version
- Shocked that WP Engine (who I usually think of as the standout WordPress host) could be using a PHP version that is below the minimum WordPress official recommendation
- Upset that I was being told by the WP Engine support team that updating is simply impossible
- Confused that with so many other hosts, upgrading to PHP 7 had been as simple as pushing a button, making sure the site didn’t break, and rolling back if something went wrong
Work hard to level up #PHP skills. Use new skills in a project. Realize @wpengine will not let you update to PHP 5.6. Get fatal error :/ #wp
— Michael Hull (@echoes_resound) September 14, 2016
Of course, WP Engine probably has a good reason for this, but all they would tell me is that they plan to update all sites to PHP 7 in the near future. Update: A WP Engine rep updated my support ticket after this post was written and informed me that since PHP 5.6 only introduces minor functionality improvements and no critical security patches, they made a choice not to update the servers on their network to 5.6.
While I’m excited for WP Engine to be updating all of their sites to PHP 7 (what a feat!), in the meantime I have had to upload my work to a different server.
My situation is no big deal at the end of the day, but I believe that this sentiment is one worth sharing:
If you operate a website or know someone who does, then on behalf of web surfers everywhere who want to stay secure, and on behalf of all developers trying to level up their PHP skills, please ask your host if you are running an officially supported PHP version.
P.S.
If you are wondering, “Wait a minute, namespaces have been around since PHP 5.3” then you are totally right. The actual fatal error that I triggered was when I tried to utilize the use function
feature that was introduced in PHP 5.6.
Leave a Reply