Spaceship Operator

In PHP 7, a new feature, spaceship operator has been introduced. It is used to compare two expressions. It returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression.

 <?php
print( 1 <=> 1);print("
");
    print( 1 <=> 2);print("
");
    print( 2 <=> 1);print("
");
?>
  

Closure call

Closures are anonymous functions that are declared inline and assigned to a variable. It can be used as a callback for later execution. In PHP 5 it was already possible to bind an object to the scope of the closure as if it was a method.

The “call” method is one of the PHP 7 features that was introduced to simplify the process.

<?php
     /**
      * Closure::call()
      */
     class PHPCode {
         private $foo = 'PHPCode';
     }
     
     $getFooCallback = function() {
         return $this->foo;
     };

     echo $getFooCallback->call(new PHPCode);

Anonymous classes

Anonymous classes are useful for simple one-off objects. With anonymous classes you can define a class and instantiate an object inline.

<?php
 /**
 Anonymous classes
 */ 
 $anonymousClassObject = new class {
     public function test() {
         return "PHPCodez";
     }
 };
 echo $anonymousClassObject->test();

Return type declarations

Whereas type hints ensure input consistency, return type declarations ensure output consistency.

We use a colon before the opening curly brace of a function to hint the return type.

The same strictness rules apply as with the type hints: if “strict mode” is disabled, return values that can be converted to the preferred type are allowed. If you enable “strict mode” this code will throw a type error.

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.

it allows developers to ensure a better input consistency of a function/method interface. By default “coercive mode” is enabled. This restricts PHP from throwing a type error when the types don’t exactly match, but when a conversion is still possible.

If you enable “strict mode” , a type error is thrown when the signatures don’t match.

PHP 7 New Features

  • Scalar type hints
  • Return type declarations
  • Anonymous classes
  • The Closure::call() method
  • Generator delegation
  • Generator return expressions
  • The null coalesce operator
  • The space ship operator
  • Throwables
  • Level support for the dirname() function
  • The Integer division function
  • Uniform variable syntax

Public interfaces

A public interface is a set of code that third-party developers can call, implement, or build as a plug-in. Magento guarantees that this code will not change in subsequent releases without a major version change.

Public interfaces for a module are marked with @api annotation.