PHP

Type declaration and return type declaration

A loosely typed language doesn’t offer the best performance since its compiler has more work to do when trying to determine the data types of its variables. On the other hand, the strict type languages consume less memory and give better performance at runtime than loosely typed languages. PHP 7 now offers the possibility to strict type variables and function signatures, known as “Type declarations” (“type hints” in PHP 5) and “Return type declarations”.

Type declarations / Type hinting

As of PHP 7 Type hinting is being referred to as Type declaration. PHP allows the developer to be more specific about what functions are getting and returning. You can optionally specify the type of parameters that the function needs and the type of result the function will return. To specify a type declaration, the type name should be added before the parameter name. Let’s first see an example:

<?php
function printMe( int $var ) {
 echo $var;
}

$v = 200;
printMe( $v );

In the above example we specified that the parameter $var being passed to the printMe() function must be an int (integer).

Return type declarations

PHP 7 also support for “Return type declarations”. Similarly to “Type declarations”, return type declarations specify the type of the value that will be returned from a function. To specify a return type declaration, the type name (followed by a colon :) should be added after the function’s closing parenthesis . Let’s first see an example:

function modulus(int $a, int $b) : int {
 return $a % $b;
}

$m = modulus ( 52 , 12 ); // 4

Error handling

If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.

In following example we’ll pass the incorrect data type, string:

<?php
function printMe(int $var) {
echo $var;
}

$v = "200";
printMe( $v );

The above example prints the following error on screen:

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

Valid data types

We can use the following data types for “type declarations” and “return type declarations”:

Class or Interface example

The parameter must be an instanceof the given class or interface name. To demonstrate, first we create a class User:

<?php
class User {
 private $fname = null;
 private $lname = null;
 function __construct($f, $l){
  $this->fname = $f;
  $this->lname = $l;
 }
 public function getFirstName() {
  return $this->fname; 
 } 
 public function getLastName() {
  return $this->lname;
 }
}

Next, we create a function to combine the first and last names with type declared parameter of User class:

function getFirstLastName (User $u) : string {
 return $u->getFirstName() .' '.
        $u->getLastName();
}

Now we’ll create a new instance of User class and pass it to getFirstLastName function:

$user = new User('Brain','Bell');
echo getFirstLastName($user);
//Brain Bell

Self example

The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods.

Array example

function findInArray(array $arr) : array {
 foreach ($arr as $k => $v) {
  if ($v == 'something') {
   $arr[$k] = 'replaced';
  }
 }
 return $arr;
}

Callable example

function cbExample(callable $cb) : callable {
 $a = $cb();
 return function ($b) use ($a) {return $a + $b);};
}

Boolean and Integer example

function boolIntExample(int $val ) : bool {
 if ($val == 0)
  return false;
 return true;
}

Float and String example

function floatStringExm(float $val ) : string {
 return "Total: $val";
}

Iterable example

The parameter must be either an array or an instanceof Traversable:

function itrExample(iterable $itr) {
 foreach ($itr as $v) {
  echo "$v ";
 }
}

function test(){
 yield 'brain ';
 yield 'bell';
}

$itr = test();

itrExample($itr);
Advertisement:
Advertisement: