One of the most significant features of PHP 7 is Improved performance: up to twice as fast as PHP 5.6. For years, developers have been asking for it. PHP 7.0.0 took its first steps toward becoming a strong/strictly typed language by providing features such as:

  • Return Type Declarations
  • Scalar Type Declarations

Other significant features of PHP 7 are:

  • Consistent 64-bit support
  • Anonymous Classes
  • The null coalescing operator (??)
  • Combined comparison Operator (<=>)
  • improved Exception Handling

Return Type Declarations

PHP is finally getting return types. we now can tell Php that the function that we’ve defined must return a value of defined type.

Fortunately, this is also possible with PHP7, but we must first enforce ‘strict’ type checking.

You may only enable strict type checking on a file-by-file basis. This means, that you’ll have to enable it for every file in which you’d like to use strict types.

To use strict types, the following declaration must be the first snippet of PHP code to appear after the opening {
public function roar() : string;
}

In a previous chapter, I warned you that type-hinting methods and functions with scalar types (int, string, etc) was impossible? Great, forget that!

With PHP7 you can type hint… ANYTHING.

Excited? I know you are!

return $seven;
}

This is perfectly valid with PHP7.

Null Coalesce ?? Operator

In PHP7, we have the Null Coalesce ?? Operator, which looks like this.

If the value to the left of the ?? doesn’t exist, or is null, then the statement will use the value to the right of the operator. Nice and short, isn’t it?

Not only that, but you can stack as many of these operators into a statement as you desire. For example.

Here, PHP7 will work its way from left to right using the first value which is valid. I’m sure that this feature will become part of your toolkit in no time at all!

Spaceship Operator

Ground control to Major Tom…

The spaceship operator makes it really simple to compare values. Let’s take a look at an example.

As you can see, the spaceship <=> operator (and yes, that’s the real name) looks like a spaceship. Clearly. Shouldn’t it be called the UFO operator?

Anyhow, in the above example the value of $tom will be set to -1. This is because 3 is less than 4. Let’s take a look at the possible values.

Define Constant Arrays

We’ve discovered how to define global constants in a previous chapter. For example.

Well, in PHP7 you can also define constants that are arrays. Here’s an example.

It’s that simple! Well what did you expect? Not all of the features are going to be major, are they!?

Anonymous Classes

Anonymous classes are classes without names. Surprised? No? Oh, ok..

Here’s an example.

In the above example, we’re passing an anonymous class into a function. See? It has no name!

Anonymous classes are useful for when you don’t need to keep a reference (assignment to a variable) to the class. These types of classes are extremely useful when combined with unit testing, which is a concept that will be covered in a future title!

Group Use Declarations

Finally, let’s take a look at Group Use Declarations.

Previously, to use multiple classes held within the same namespace we’d have to use them separately, like this.

use Animal\Panda\Giant;
use Animal\Panda\Fluffy;
In PHP7, you’ve got another option. Here’s an example.

The effect is identical, but you’ve saved yourself a few keystrokes. It’s worth noting that you can still use class aliases within the group { use } brackets. For example.

While some will prefer to use this format. I would actually encourage the use of the ‘older’ method of using multiple classes within the same namespace. You see, if using a version control system, it will be much easier to determine whether a class is removed, or to commit a change without occurring conflicts with team-workers code. I also think it’s much clearer to read!

We’ve not covered version control in this title, so don’t panic if the above statement doesn’t make sense! Use whichever format works well for you. It’s great to have options!

Leave a Reply