This PHP 7 tutorial help to understand type hinting in PHP 7.The Type Hinting help to define arguments data type at the time of function declaration. You will get the fatal exception if the required data type variable not passed into the method.
The PHP 5 also has type hinting but did not support scaler type. The PHP 7 support type hinting along with scalar data type.
What is a scaler Data type in PHP 7?
All of the data types which hold a single data item, those types are called scaler data types. The PHP 7 has char, int, short long, float, and double scalar (or base) data types.
You can also check variable is scaler type or not using is_scalar() method.
PHP5 does not support type hinting to basic or scaler data types like string, integers, float etc. You can validate passed argument data type using “is_” family functions, Like is_string, is_integer etc.
Lest Start PHP 7 Type Hinting with Example:
We will create a simple example to demonstrate type hinting in PHP 7, I will create a type hinting example based on integer, object and array.
Type Hinting in Scalar Data Type
<?php function integerTest(int $val) { echo $val; } integerTest(1);
Run the above program using PHP 7, You will get ‘1’ output.
If you run the same above code into PHP5.6, Then You will get the below PHP fatal error:
Error Argument 1 passed to
integerTest()
must be an instance of integer, integer given, called in /test.php
PHP Type Hinting in Array Type
Lets a create a PHP function that will take arguments of the array type. I have the below function that takes $vals
variable as a parameter and it must be an array data type.
function arrayTest(array $vals) { print_r($vals); }
Now, We will be called the above method using integer data type:arrayTest(1);
When you passed an integer value into the above method, You will get the below fatal error message:
FATAL ERROR Uncaught TypeError: Argument 1 passed to arrayTest() must be of the type array, integer given,
The above message clearly tells us: We have passed integer value but the function expected array type argument.
Now we will call using array data type arguments:
arrayTest(array(3,4,5));
The output:Array ( [0] => 3 [1] => 4 [2] => 5 )
Type Hinting in Object Type
We can also do type hinting into the object type. Created a new Employee
Class and passed the Person
object as an argument into the constructor method.
class Employee { protected $name; public function __construct(Person $per) { $this->name = $per->name; } public function getName() { return $this->name; } } $emp = new Employee(1);
Now, We will supply an integer value to the Employee Class.p1 = new Employee(1);
The output would be the fatal error:
FATAL ERROR Uncaught TypeError: Argument 1 passed to Employee::__construct() must be an instance of Person, integer given.
Let’s passed the required arguments, which is the Person
class object, We will create a Person
class and set name into this object:
Class Person { public $name; public function __construct(string $name) { $this->name = $name; } }; $per = new Person('parvez'); $emp = new Employee($per); echo $emp->getName();
The output is:parvez
Conclusion
This tutorial help to understand type hinting for PHP7 as well as with php 5. I have explained type hinting for the scalar types (strings,integers etc), array and object types. I hope its helps you to understand type hinting in php7.
thanks a lot for this article