Home >> Latest Articles >> Type hinting in php 5

Type hinting in php 5

Type hinting in php 5: Type hinting provide facility by which we can specify the expected data type (arrays, objects, interface, etc.) for an argument in a function declaration.

Type hinting in php 5 and 7 can be most advantageous because it results in better code organization and improved error messages.

How to do array type hinting?

When we would like to force a function to get only arguments of the type array, we can put the keyword array in front of the argument name, with the following syntax:

function myFunction(array $parameter) {
 //code block
}

#Example (working)

function myFunction(array $parameter) {
 foreach($parameter as $data){
 $output[] = $data;
 }
 return $output;
}
$result = myFunction([11,22,21,1,21]);
print_r($result);

//Output:
Array ( [0] => 11 [1] => 22 [2] => 21 [3] => 1 [4] => 21 )
Note:

if you define the input parameter as an array and pass something else while function calls it to show fatal error.

#Example with Error:
function myFunction(array $parameter) {
 foreach($parameter as $data){
 $output[] = $data;
 }
 return $output;
}
$result = myFunction("testdata");
print_r($result);

//Output:
Fatal error: Uncaught TypeError: Argument 1 passed to myFunction() must be of the type array, string given, 
You may also like: OOPS Interview Questions and Answers
How to do object type hinting?

Type hinting can also be used to force a function to get an argument of type Object. For this purpose, we put the name of the class in front of the argument name in the function.

In the following example, the class's constructor can only get objects that were created from the Driver class. We ensure this by putting the word Driver in front of the argument name in the constructor.

class Car {
  protected $driver;
  // The constructor can only get Driver objects as arguments.
  public function __construct(Driver $driver)
  {
    $this->driver = $driver;
  }
}
 
class Driver {}
$driverObj = new Driver();
$carObj = new Car($driverObj);

Does PHP support type hinting to basic data types?

It depends on php version.

PHP 5 does not support type hinting to basic data types like integers, floats, Boolean or strings. So, when we need to validate that an argument belongs to a basic data type, we can use one of PHP's "is_" family functions.
#Example:

is_bool - to find out whether a variable is a boolean (true or false).

is_int - to find out whether a variable is an integer.

is_float - to find out whether a variable is a float (3.14, 1.2e3 or 3E-10).

is_null - to find out whether a variable is null.

is_string - to find out whether a variable is a string.

On the other hand, PHP7 does support scalar type hinting. The supported types are integers, floats, strings, and booleans.
You may also like: Core PHP Interview Questions Answers
#Example: In PHP7 following example will work fine, but it will give an error in php 5 or less.
function stringTestFunction(string $string) {
 echo $string;
}
echo stringTestFunction("Yes, it is a string");

//Output: 
Note:
PHP7 => Yes, it is a string
PHP5 => Argument 1 passed to stringTestFunction() must be an instance of string, string given
Full Stack Tutorials

Author | Blogger @FullStackTutorials | View all articles