PHP is a flexible, dynamic language that supports a variety of programming techniques. It has evolved dramatically over the years, notably adding a solid object-oriented model in PHP 5.0 (2004), anonymous functions and namespaces in PHP 5.3 (2009), and traits in PHP 5.4 (2012).

Php has evolved from a procedural language to an object-oriented programming. It is still not a complete objected oriented programming language but it has come quite a far way to be one.

I am not going to discuss all of the features of PHP 7  but I will touch upon a few very important ones. Below is the list of features i am going to discuss and a short description of what they do.

Performance

Let’s not forget to mention that the performance of PHP 7 is supposed to be impressive. Some say that it’s twice as fast as PHP 5(.6) others use even more dazzling numbers. I’m confident that it performs great and I’m curious to see the average numbers once people start using it.

Scalar type hints

Type hints have been available in PHP for while now. Unfortunately, they were restricted to classes, arrays and callables.As of PHP 7, the scalar types (integers, floating point numbers, booleans and strings) can also be used as type hints.

<?php
/**
 * Scalar type declarations
 */

//declare(strict_types=1);
function add(int $a, int $b) {
    return $a + $b;
}

var_dump(add(1,2));
var_dump(add("1","2"));

Return Type Declarations

PHP 7 also supports Return Type Declarations which support all the same types as arguments. To specify the return type, we add a colon and then the type right before the opening curly bracket.

function getTotal(float $a, float $b) : float {

If we specify the return type of float, it will work exactly like it has been in the previous 2 examples since the type being returned was already a float. Adding the return type allows you to to be sure your function returns what is expected as well as making it easy to see upfront how the function works.

Anonymous classes

Anonymous classes are useful when simple, one-off objects need to be created.With anonymous classes, you can define a class and instantiate an object inline.

<?php

// Pre PHP 7 code
class Logger
{
    public function log($msg)
    {
        echo $msg;
    }
}

$util->setLogger(new Logger());

// PHP 7+ code
$util->setLogger(new class {
    public function log($msg)
    {
        echo $msg;
    }
});

Spaceship Operator

PHP 7 also brings us some new operators. The first one we’re going to explore is the spaceship operator. With a name like that, who doesn’t want to use it? The spaceship operator, or Combined Comparison Operator, is a nice addition to the language, complementing the greater-than and less-than operators.

$compare = 2 <=> 1
2 < 1? return -1
2 = 1? return 0
2 > 1? return 1

Null Coalesce Operator

Another new operator, the Null Coalesce Operator, is effectively the fabled if-set-or. It will return the left operand if it is not NULL, otherwise it will return the right. The important thing is that it will not raise a notice if the left operand is a non-existent variable.

$name = $firstName ??  "Guest";

The Integer division function

Maybe not one the most important PHP 7 features, but still worth mentioning: the intdiv function returns the integer value of a division whereas regular divisions can result in a float being returned.

<?php
/**
 * Integer division
 */
var_dump(
    intdiv(10, 3),
    (10/3)
);

 

2 thoughts on “PHP 7 and its features

Leave a Reply

Your email address will not be published. Required fields are marked *