PHP 5.4 has been percolating the web now for a few months and I wanted to dive in deep and understand what the big changes are for my longtime favorite coding language. I do like java a lot and Ruby is looking more and more promising, but at the end of the day, PHP is my bread and butter language.
Here are the top five changes you should know about in PHP 5.4 and why they’re important.
- Traits
Since PHP began supporting Object oriented development back in version 4, there has been a major limitation that has limited it from being considered a true blue object oriented language. My Java programming professor outlined the three pillars of basic OO design as:- Encapsulation
- Polymorphism
- Inheritance
So far, PHP has done a good job of supporting both encapsulation (being able to build discreet and self-contained class objects) and Polymorphism (being able to extend a class into many types of additional forms). The last point of inheritance, being able to take on the characteristics of a parent class and extend them) was limited to a one to one relationship. Meaning a child class could only extend a single parent class. Other languages like Java and C# allow you to inherit from multiple parents.To solve this problem, PHP 5.4 introduces Traits. Traits allow you to create what amounts to abstract parent classes that can be utilized by child classes. And the good thing about Traits is that you can utilize more than one per class.
Here’s an example:
trait TestTrait { public function testMethod() { echo("Traits are very cool!"); } } class Example { use TestTrait; public function showOutput() { $this->testMethod(); } } $ex = new Example(); Example()->showOutput();
This would produce the line:
Traits are very cool!
So PHP 5.4 brings PHP closer to the realm of complete OO implementation that before.
Some cool things about traits:
- They can be overridden by class implementations
- You can test if a trait exists similar to testing if a class exists, get a list of loaded traits and even test if an object is a trait.
- Additional Array Functionality
Two new functional additions have been added for working with arrays. The first is one that I am glad to see and have been hoping for a long time, Dereferencing Arrays. What this allows you to do is to access the children of an array directly off of a function or method call. This allows you to turn two (or more) lines of code into one. And who doesn’t like that?Let’s see it in action.echo aFunctionWithReturnArray()['attribute'][0]
It’s not a huge change, But a welcome one.
The other change for arrays is the ability to use bracket syntax to define an array. This brings PHP in line with other languages, most notably JavaScript, which I pair PHP with quite often. This encourages code consistency among languages and could provide some small but important benefits to standard code development.
var $newArray = array('1','2',3,4,5); var $newArray = ['1','2',3,4,5];
Simple, but still effective.
One last change in PHP 5.4 is that if you attempt to convert an array into a string, PHP will throw an E_NOTICE
level error.
- Removed Functions (finally)
Three features that once were often used but have fallen out of favor in recent years have been removed in PHP 5.4. These include:- register_globals
- Safe Mode
- Magic Quotes
- Call time pass by reference
Those who are new to PHP may not have experienced the “joy” of developing with these features but as a PHP developer since version 2 (Yes ‘m dating myself, get over it!), it’s a welcome change to see these removed.
Safe Mode and Magic Quotes were intended to help add layers of security to PHP applications built on shared hosting services and combat unscrupulous activity with “virtual” layers of protection. All they really did was add layers of headaches and additional man hours of debugging with the problems they caused.
Register Globals originally gave developer’s access to GET and POST data by automatically parsing them into global PHP variables. This also led to some issues as uninstantiated variables could be used to exploit insecure PHP applications. Their use was replaced by the $_GET and $_POST superglobals in PHP 4 and now the nightmare of register_globals comes to an end in PHP 5.4.
- Additional Session Functions
Several existing session functions such as session_register, session_unregister and session_is_registeredhave been removed. But PHP 5.4 has added some new (and good ones) as well.Session_statusallows you to get information back about the current session (or lack thereof) and call also tell you if session are enabled or not (good for distributed applications). This gives you better access and information about the session that the $_SESSION superglobal.
SessionHandler is a class that can be extended and used to build custom session handling objects and additional session functionality.
File Upload Progress Tracking is now possible without the use of workarounds like the APC thanks to several new INI directives for upload_progress. Once set up, the progress can be accessed directly from the $_SESSION Superglobal object in a function that can be pinged via AJAX requests.
- Tag Along Web Server
For as long as I can remember, I’ve always had an apache web server with MySQL running on my Windows desktop to develop my web apps. It’s caused a few headaches when later deploying to Linux, but a few tips and tricks here and there have mitigated any issues.For those not interested in going through installing Apache and MySQL or a bundled desktop server like WAMP/MAMP or VertrigoServ, PHP 5.4 now features its own built in Web server. This adds the ability to quickly launch and test your PHP code locally.
It also features a command line interface so you can run PHP commands and test code directly against the server.You can also easily access a command line interface to make call directly against the PHP interpreter. This is not a huge addition, but for many PHP developers, it a welcome one.
So that’s a quick review of the top new features in PHP 5.4. There’s a lot more included and I recommend viewing Mike Stowe‘s excellent and fun “What’s New in PHP 5.4?” Presentation on SlideShare.net.