TechTip: PHP 7.0 Is Coming, Part 2

Scripting
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times


PHP 7 is coming, but should you run toward it or run for your life? In Part 1 last month, we looked at the history of this release. Now let's get down to the nitty gritty and see what it's got for us.

 

 

If you have been eagerly awaiting the next big release of PHP, your wait is about over. Scheduled for delivery before the end of the year (tentatively, October 2015), the new release is just about here. Last month, we talked about the biggest enhancement that it bringsnamely, honking big improvements in speedbut there are a ton of other things in there as well, so let's buckle down and take a look at some of them.

Before doing that, however, I want to remind you how you get the new version of PHP. Unless you are a super tech and have rolled your own PHP stack, you can't just download PHP 7 to your laptop and be off to the races. Remember, PHP is a server -side language. It doesn't run on your laptop; it runs on a server that your laptop accesses. As a result, you have to wait for whoever is controlling the server to incorporate PHP 7. (Note: if you're a super tech, you can get a download of PHP 7 for Ubuntu 14.04 and CentOS 7 right now.)

If you're using Zend, then you need a release of the Zend Server that has PHP 7 in it. Zend hasn't announced when that will happen (PHP 7 being incorporated into a release of Zend Server), but the word on the street is maybe Q4 of 2015. You didn't hear that from me, of course.

Spaceship Operator

Who says techies are Star Wars geeks? It's probably just a coincidence that the new operator that is being offered, the <=>, happens to look like the Empire's TIE Fighter. And it's also probably coincidence that the new operator is being referred to as the Spaceship Operator. How does it work?

Very simple. It compares two values but does it in terms of a three-way comparison. That is, starting with a <=> b.

  • If a == b, then the operator returns a 0 (remember, in PHP a single = means assignment and a double == means equivalency on a comparison). 
  • If a > b or a >= b, then the operator returns a 1.
  • If a < b or a <= b, then the operator returns a -1.

The main use of this operator is for ordering (sorting) rows. In the past, certain types of ordering were very difficult to do in a straightforward manner (particularly if multiple columns were involved). Use of this operator will now make the problem much simpler. Consider this example that is taken from the RFC proposal for this operator.

It makes writing ordering callbacks for use with usort() easier. Commonly, users write poor or incorrect ordering functions like this one:

function order_func($a, $b) {

   return $a >= $b;

}

When users do write correct ordering functions, they have to be quite verbose:

function order_func($a, $b) {

   return ($a < $b) ? -1 : (($a > $b) ? 1 : 0);

}

This becomes particularly bad when sorting by multiple columns.

With this operator, you can easily write proper ordering functions, like this one:

function order_func($a, $b) {

   return $a <=> $b;

}

Sorting by multiple columns is simpler now, too:

function order_func($a, $b) {

   return [$a->x, $a->y, $a->foo] <=> [$b->x, $b->y, $b->foo];

}

Null Coalesce Operator

Many times in PHP we ask the question: does this thing exist? And based on the answer, we may do one thing or another.

Generally, this is handled via the ISSET, for example:

isset($_GET['variable']) ? $_GET['variable'] : ""

The problem with this is that it's a bit lengthy. You can use the ternary or "Elvis" operator (?:) to shorten that up:

$_GET['variable'] ?: ""

This evaluates correctly, but then you get an E_NOTICE, a PHP runtime error, if the variable does not exist, which it might very well not.

But the new Null Coalesce operator (??) lets you do this very easily without worrying about any messages popping up. In the case below, it checks for the existence of a variable, and if it does not exist, then the second parameter (in this case, 'Alaska') will be used for the variable value.

$username = $_GET['variable'] ?? 'Alaska';

Anonymous Classes

Generally, when a class is defined in a PHP script, it's given a name by the programmer, but Anonymous Classes don't have a programmer-defined name. The program will create a name for the class based on some sort of structure, but none is mentioned in the class definition. Anonymous classes are a part of both Java and C# but have not been available in PHP up to this point.

Why would you want to use an Anonymous class? Basically, it's a shortcut. You would use an anonymous class the same way you would a local variable, something that's limited in scope and is probably going to be used only once. It also give you the ability to both define and create an object in the class (instantiate) in one statement.

Additional Reserved Words

The following words can no longer be used in class, interface, or trait names: int, float, bool, string, true, false, and null.

I guess the main thing here is that this demonstrates one negative thing that can come out of a new version of PHPnamely, that it can break scripts that you already have. That is, if you currently have those words in classes, etc. for existing scripts, then they will fail when running in PHP 7.

And That's Not All

No, that's not all by a long shot. There are probably about three dozen different enhancements to PHP in 7, but I'll leave it to you to take a look at what they all are. In addition, Zend is offering a PHP 7 Jump Start Class. It's an online class with a number of sessions this summer and is probably the best way for most people to get a good feel for what 7 has to offer. Enjoy.

 

BLOG COMMENTS POWERED BY DISQUS