PHP

Get the Hang of PHP 7


Long before I started looking into PHP, I disliked it particularly for two main reasons; the supposed “shittiness” and  the dynamic typing it offered, just like other languages like Javascript, ruby, etc. This particular bias was because I was coming from Java, where form and  structure are “commandments” you have to obey or be marked for garbage collection(pun intended).

When I eventually started looking into PHP, I was struck by the close resemblance, except for a few adventurous design decisions it bore to Java. Some things were off though, I had a tough time designing my applications, when I knew I had to do without a few things like return types’ for methods, scalar types’ or  primitive types’ (like Java calls them). Dependency injection came in handy on some occasions, but wielding the power of  ‘polymorphism’ seemed to be a Herculean task.

Enter PHP 7

When PHP announced the release of PHP7, I was particularly excited because of the new features that were added, feature like:

1. Combined comparison operator  (<=>) a.k.a Spaceship Operator

2. The null coalescing operator ( ?? )

3. Namespace grouping

4. Anonymous Classes

5. Scalar type Declarations

6. Return type Declarations

7. Uniform Variable  Syntax

8. Fatal Errors transformed to Exceptions

And more…

I will walk us through some of the most exciting features of the all new PHP7. Brace yourself for the awesomeness.

1. The Combined Comparison Operator ( <=> ) Spaceship Operator

Before PHP7, if you wanted to sort the values of an array of persons say

You would probably do something like this:

In PHP7, you don’t have to use different functions for sorting in order and reverse order, better still, you can sort more than arrays. You would achieve the same thing like this;

To sort in reverse order, you just change the order of the arguments passed to the ‘spaceship’ operator, like so;

Just to be very clear, if you really do not understand, this is the underlying principle

If the number on the right is greater, then the result of the evaluation is -1.

If the number on the left is greater, then the result of the evaluation is 1.

If the numbers are equal on both sides, then the result of the evaluation is 0.

2. The null coalescing Operator

The null coalescing operator is used to test for the ‘nullity’ or not of a variable or object. Before PHP 7, you had to use the isset($var) to test for nulls.

If you had a variable say $age and you wanted to test if the variable had been set, you probably would do something like this:

In PHP7, however, we can save some keystrokes with the null coalescing operator;

That returns the age variable if it exists and is not null, otherwise it returns 21.

Pretty straightforward right?..Yes, it is :smile:

3. Namespace Grouping

It is now possible to group your namespaces. Assuming you had a number of namespaces that all have a common prefix, but you don’t want to write them all out on separate lines. You are welcome, PHP7 provides you with a namespace grouping functionality, where you can compress this;

will become:

4. Anonymous Classes

Just as the name implies, they are classes without names. This allows you to create classes on the fly, just like with closures, the instance is immediately created and assigned to a variable : the object.

Before now, you had no option but to write a piece of code that would look like this for a Logger.

In PHP7, you can skip naming the TextMessage class and simply assign its instance to an object at declaration time directly like so:

5. Scalar Type hints and Declarations

Before PHP7, You could type hint arraysobjects in methods and class constructors,  a concept used by DI (Dependency Injection)  containers, but we had no way of type hinting variables or Scalar type hinting, as it is called. PHP7 to the rescue. Now we can fortify our methods and variables, we can ensure that only what we intended our methods to be used for will be done. In other versions of PHP, you could declare a method add($a, $b)  which will receive both doubles, floats and integers. This is a problem because the code could generate unpredictable results which could cause your software to be rickety, plus, you had to do additional error checking to make your software the least secure.

Before PHP7:

PHP7:

Now, we have type-hinted the arguments to be integers. So if someone calls the add function and doesn’t pass integers as arguments, an exception would be thrown.

6. Enforcing Strict Type 

In Javascript, when you want to enforce strict standards like always declaring a variable with var keyword and appending semicolons to the end of a statement, you can do something like this:

In PHP 7:

You can now enforce strict standards like so:

7. Return type declarations

Methods can now have return types using the scalar types. You could restrict the type returned by your methods. This takes care of surprise and unpredictable results.

Scalar type hints also work for return types.

When you set the declare(strict_types=1)  on the above snippet, passing in the wrong argument types will cause a ‘catchable fatal error’.

8. Uniform variable syntax

Uniform variable syntax provides a simple way of directly accessing a closure within a function with one call. Say you have a function;

You can call the closure like this:

9. Throwable Interface

PHP7  has  introduced exceptions as a replacement for fatal or recoverable fatal errors. These exceptions do not extend Exception but instead extend a new class BaseException. They include:

EngineException, ParseException and TypeException.

To use the exceptions, the proper type hint has to be  provided. Consider the code snippet below:

The above TypeException will not be caught, instead a fatal error would be thrown like so:

//Fatal error: Uncaught TypeException: Argument 1 passed to add() must be of the type integer, //string given.

To catch the exception, the proper type hint has to be used;

These and many more features have been added to PHP7. In future posts, We will explore more into the awesome world of PHP7. For now, keep the flame burning. :smile:

We earnestly wait for the official release on December 3, 2015.

Fire up your vagrant and use php7dev box to test everything and start basking in the euphoria of sweet goodness!!!

Please if you have any questions or observations, let me know in the comments section below.

 

About Verem Dugeri