Introduction
PHP is growing up fast. There has been a major push from the folks at Zend (the company that maintains PHP), and this focuses on two major concerns; security, and the object oriented aspects of the language. We’ve come a long way from PHP 4, which introduced a very simple and shaky object oriented interface for developers to play with. But PHP 5 changed that, and with every increment of the PHP 5 version, we are being given a great deal more functionality, as well as a new level of maturity.
What’s new? Let’s check out a few of the new features.
Traits
Traits have been added to PHP for a very simple reason: PHP does not support multiple inheritance. Simply put, a class cannot extends more than on class at a time. This becomes laborious when you need functionality declared in two different classes that are used by other classes as well, and the result is that you would have to repeat code in order to get the job done without tangling yourself up in a mist of cobwebs.
Enter traits. These allow us to declare a type of class that contains methods that can be reused. Better still, their methods can be directly injected into any class you use, and you can use multiple traits in the same class. Let’s look at a simple Hello World example.
<?php
trait SayHello
{
private function hello()
{
return "Hello ";
}
private function world()
{
return "World";
}
}
trait Talk
{
private function speak()
{
echo $this->hello() . $this->world();
}
}
class HelloWorld
{
use SayHello;
use Talk;
public function __construct()
{
$this->speak();
}
}
$message = new HelloWorld(); // returns "Hello World";
As can be seen in the example above, the traits ‘Hello’ and ‘Speak’ has methods (functions) that automatically become part of the class ‘HelloWorld’. I was even able to make the methods ‘hello’, ‘world’, and ‘speak’ private. As you know, a private method can only be called from the class it is declared in, but here, because we are using traits, this is allowed. PHP treats the methods in the traits as methods in the class using the traits. Put more simply, methods in a trait are included in the class using the methods. This gives you a lot of flexibility, as you can see.
Short Array Syntax
You now have a short array syntax available, and the idea is very simple. Consider the array:
$cars = array('Porsche', 'Ferrari', 'Volkswagen');
You now have the option of using the method in the same way as Javascript’s literal array notation:
$cars = ['Porsche', 'Ferrari', 'Volkswagen'];
And for an associative array:
$cars = ['cheap' => 'Volkswagen', 'expensive' => 'Ferrari'];
Array Dereferencing
Consider the following example:
$string = 'this,is,a,string';
Suppose we want to find the value of the second part of that string. Before PHP 5.4, we would have to say:
$explode = explode(",", $string);
$second = $explode[1]; // $second now has a value of 'is'
But this is no longer needed:
$second = explode(",", $string)[1]; // $second now has a value of 'is'
I’m a realist, and realistically you cannot be sure that the string has more than one part (or any parts at all, but I’m assuming you’ve already checked for that), and you cannot be sure that part is not empty (I’m assuming you checked that out too). I would argue this might be something – if not used correctly – that could lead to horrible error messages (or functionality inconsistencies) popping up. Be aware when using them.
Anonymous Functions Now Support $this
Also known as closures, anonymous function now support $this. Before PHP 5.4, this could be done in a rather clumsy way:
function anonymous()
$that = $this; // $that now is $this return function() use ($that) { $that->hello(); }; }{
Now, however, we can say something like:
class One
{
function hello() {
echo 'Hello World!';
}
function anonymous()
{
return function() {
$this->hello(); // $this wasn't possible before
};
}
}
class Two
{
function __construct(One $o) // object of class One typehint
{
$x = $o->anonymous(); // get One::hello()
$x(); // execute One::hello()
}
}
new Two(new One); // return 'Hello World!'
PHP Short Tag Is Always On
Previously, the PHP short tag (<?=) could be turned on and off in your php.ini file. This tag is now always active, regardless of the php.ini setting.
Binary Numbers
Binary numbers are now supported. Integers can be assigned in decimal (base 10), hexadecimal (base 16), octal (base 8), or binary (base 2) notation, and the optional sign (+ or -).
Callable Typehinting
PHP is, by definition, a weakly typed language. Typehinting was incorporated in order to make PHP a stronger typed language, however, types could (until PHP5.4) only be objects or arrays. Strings and Ints are not yet supported, but we now can perform callable typehinting.
function some_function(callable $a)
{
return $a();
}
function some_callback_function(){return 'Hello World!';}
class Hello{static function hi(){return 'Hello World!';}}
class Hi{function hello(){return 'Hello World!';}}
echo some_function(function(){return 'Hello World!';}); // anonymous function
echo some_function('some_callback_function'); // callback function
echo some_function(['Hello', 'hi']); // class name, static method
echo some_function([(new Hi), 'hello']); // class object, method name
High Precision Timer
PHP 5.4 introduces a timer to the session array, which can be used to measure things like script execution, as below:
echo 'Execution time: ', round(microtime(true) - $_SERVER['REQUEST_TIME_FLOAT'], 2) . ' seconds';
CLI Webserver
PHP 5.4 gives us a simple test server, which is started from the command line. This is for development only, and should never be used in production, but the idea is that it is simple to test PHP applications without having to setup things like virtual hosts.
To use the built in server, simply navigate to the directory containing your PHP file or index.php file of your application, and run:
$ php -S 0.0.0.0:8080 -t {filename}.php
Then open the url http://0.0.0.0:8080 in your browser and watch your application run.
Conclusion
PHP 5.4 has added a lot of functionality to the language. As with all new things, however, we must make sure we fully understand the implications of the features we are using properly, and while that has not been the focus of this article, please remember to go to php.net and make sure you are comfortable with what you are doing.