Post Format

PHP7, are you ready for it?

Leave a Reply

You guys may very well be aware of the fact that PHP7 is coming by the end of this year which is something very significant to every person out there who uses PHP. May it be a freelancer, a student or a company, everyone right now is on about PHP7’s release.

1. Scalar type declarations. You guys may be aware that Scalar type declaration is a feature of PHP7, but what really is it? Well, with this feature, you can now type hint data types where-as in previous versions of PHP, it was only possible to type hint classes and arrays.

2. Return type declarations. In PHP7, not only can you type hint data types, you can also define the function or method return data type. Which is the type of data your function or method is going to return after it is done executing. For example, if your function or method returns a sum of two integers, then the return type is obviously integer.

3. Null coalesce operator. With this brand new null coalesce operator, you can make your code more shorter when using ternary operators. You may do something like $age = isset($_GET['age']) ? $_GET['age'] : null); before, but you no longer need to write that much any more! All you have to do to achieve that is to write $age = $_GET['age'] ?? null; and that is it! That does the trick for you right away.

4. Spaceship operator. This is another cool operator coming in PHP7. The symbol or the notation for this operator is <=> and it is used to compare two integers, floats or strings. If both of the values are equal, then it returns a 0. If the left value is smaller than the right value, then it returns a -1 and when the left value is greater than the right value, then it returns a positive 1. You might be wondering how the strings are determined to be greater or smaller in value, well, it is determined by the ASCII value they hold.

5. Constant arrays. With this, you can define an array which is a constant using the define() function. Until PHP 5.6, it was only possible to achieve this using the “const” keyword. The syntax is as follows:

define('PEOPLE', [
    'John',
    'Doe',
    'Jack'
]);

Sounds cool, yeah?

6. Anonymous classes. You might have heard of anonymous classes. If you haven’t, then, anonymous classes are classes which are basically not defined as normal classes. They are basically just use and dispose classes. An example would be:

$dog->setSoundAdapter(
    new class {
        public function makeSound()
        {
            echo "Woof!";
        }
    }
);

So, as you see in the above code, a class is just created as well as instantiated at the same time and passed onto a method, and as soon as it serves it purpose, it disappears from the context.

Although these are not the only features going to be implemented in PHP7, I am going to stop here. If you want to know the complete list, then you can go ahead and take a look here at the PHP’s official site.

Credits: PHP Manual for information regarding PHP7.

Posted by

Hassan Althaf is a self taught programmer who has taught him self how to code by learning various languages from the internet.

Leave a Reply

Required fields are marked *.