Type Hinting in PHP
Type Hinting in PHP

Type Hinting in PHP

Type Hinting in PHP

In this Tutorial, we will learn about “Type Hinting in PHP”

Type Hinting is a programming term in which we can define types of parameters, arguments, return types of a class, parameters, and their functions. As you know there are two types of language. Static (Java, C++) and dynamic (PHP, Python)

  • In simple words, type hinting means providing type hints to function to only accept the given data type.
  • In technical word we can say that Type Hinting is a method by which we can force function to accept the desired data type.
  • Type Hinting in PHP, we can use type hinting for Object, Array, and callable data types.
  • Type hinting can help developers in any application by providing information regarding parameter types, function return types, etc.

Why should we use Type Hinting in PHP

Type Hinting will will help us to keep our code more organized and error free. It will help us by providing better error message. Also type hinting will increase the readability of the code.

Type Hinting in PHP Example:

<?php

// STUDENTS CLASS
class Students {
    public $students = array('Walter', 'White', 'Jesse', 'Pinkman');
}


// SHOW STUDENTS FUNCTION DEFINATION WHERE ARE DEFINING THE ARGUMENT TYPE
function showStudents(Students $student_list){
    print_r($student_list->students) ;
}


// CALLING showStudents METHOD, WHERE WE CAN ONLY PASS STUDENTS TYPE
showStudents(new Students());

?>

Example Explanation:

  • As you can see, there is a Students class where Students is stored as an array in $students array.
  • There is a showStudents function definition which takes argument as a class. This function receives Class as argument. If we pass any integer or array as argument, it will give us an error. Such as, Let’s say: when we calling the method showStudents(), if we pass an array as parameter , like this:
  • showStudents(array('John', 'Doe'));

it will give us an error, like: Fatal error: Uncaught TypeError: Argument 1 passed to showStudents() must be an instance of Students, array given

  • So as we type hinted that method as class argument type, we must pass class like this: showStudents(new Students()); It will give us the expected result like below:
  • Array ( [0] => Walter [1] => White [2] => Jesse [3] => Pinkman )

 

Disadvantages of Type Hinting:

  • Removes dynamicity of function arguments
  • Function/Method only take one type of Data

 

So, we can say that, Type Hinting will save us from many errors related to arguments and parameter. Type hinting can help us to catch certain types of mistakes. Also will help in the long run to work in a old/legacy codebase or to a future developer.

That’s all for today. See you in next one.

If you like this one,  please checkout my other articles.

If you like this article, please checkout our others article as well.

Laravel:

PHP:

Our YouTube Channel

 

Editorial Staff

A Learner and trying to share what I learned!