Functions Type Hinting and Void Functions in PHP 7

Overview

Since PHP5 functions had the ability to accept arrays and classes as parameter types. Now PHP 7 expands that list to bool, float, int, string. You can also set types for the return values, and allow for these values to accept a null value. Finally you can set a function as void which will prevent it from returning a value.

Return Values

Some functions return values, while some do not. For example, print returns a 1 or 0 indicating its success while echo does not. Functions that do not return a value (above examples) are called void functions.

<?php
function add_two_numbers($a, $b) {
  return $a + $b;
}

$sum = add_two_numbers(2, 2); // Stores 4 to $sum variable
$sum2 = add_two_numbers(3, 4); // Stores 7 to $sum variable

?>

In the above example the function returns a value to a variable. Functions terminate when they reach the return statement. While it is possible to have multiple return statements inside of a function (conditionals or switch statement) only one of them can be used.

Void Functions

A void function does not return a value. The following function is a void function.

<?php
  function sayHello() {
    echo 'Hello, World!';
}
?>

Returning Multiple Values With an Array

<?php

function generateRandomFirstName() {
  // code to generate name
  return $firstName;
}

function generateRandomLastName() {
  // code to generate name
  return $lastName;
}

function generateFullName() {
  $name = array();
  array_push($name, generateRandomFirstName(), generateRandomLastName());
  return $name;
}

list($firstName, $lastName) = generateFullName();

echo "Hello, $firstName $lastName.";



You can only return one value, but if you need to return multiple values, use an array. The example above demonstrates using the list function to return multiple values. Keep in mind if you need to use an array, just return that and don’t use list().

Making Function Arguments Optional

You can make function arguments optional to the client programmer by setting a default value in the parenthesis.

function bakeACake($glutenAllergy = false) {
    if (!$glutenAllergy) {
      echo 'Cake is done. Come eat it.';
    } else {
      echo 'You cannot have a cake because you are allergic to Gluten.';
    }
  }

  bakeACake(); // Outputs 'Cake is done. Come eat it.
  bakeACake(true); //Outputs You cannot have a cake because you are allergic to Gluten.

These ‘optional’ parameters must come after all required parameters.

Setting a Type for Parameters

PHP has the ability to declare types for the following values: class, array, bool, float, int, and string. This is pretty straightforward – the type precedes the variable.

function test(bool $testing) { } 

You can also provide that the argument can accept a null value. To do this put a ? in front of the type.

function test(?bool $testing) { } 

 

Also it should be noted that PHP will by default try to automatically type cast a variable. Consider the following code:

<?php
function greet(string $name) {
  echo "Hello, $name!";
}

greet(12); // Outputs Hello, 12!

To stop PHP from doing this add the following code to the top of your script

declare(strict_types=1);

Declaring Return Type

Return types can be declared in PHP as seen below:

<?php
function greet(string $name): string {
  return "Hello, $name!";
}

Additionally you can allow the function to return null also by adding a question mark in front of the return type

<?php
function greet(string $name): ?string {
  return null;
}

Void Return Type

<?php

function void_one: void {
  return 1; // Fatal error: a void function must not return a value
}

function void_two: void {
  // valid
}

function void_three: void {
  return; // valid
}

function void_four: void {
  return null; // Fatal error: a void function must not return a value
}


As seen above PHP 7.1 supports a void return type.

 

Published by burnedfaceless

Brian Abbott is studying IT and Music Composition at Georgia Southern University.

Leave a comment

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