---
title: "Type Hinting in PHP"  
description: "Type Hinting is a technique that hints the function to accept only given data types. In PHP we can use type hinting for array, object and callable dat"  
author: "Anonymous User"  
published: 2016-06-21  
updated: 2018-03-15  
canonical: https://www.mindstick.com/blog/11164/type-hinting-in-php  
category: "php"  
tags: ["php"]  
reading_time: 3 minutes  

---

# Type Hinting in PHP

Type Hinting is a [technique](https://answers.mindstick.com/qa/93922/why-c-sharp-programmers-use-properties-technique-in-c-sharp-programming) that hints the [function](https://www.mindstick.com/articles/13001/multi-statement-table-valued-user-defined-function-in-sql-server) to **accept only given data types**. In PHP we can use type hinting for array, object and callable data type. Type hinting is introduced from PHP5. Basically, type hinting is a method that we can specify expected data (array, object, [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces), etc) for an argument in a function declaration. It is advantageous because it will give result in better way and improve the [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core). In this part we will discuss about how in PHP 5 type hinting works.

**Following is a class/object example:**

In the below example text() is a class and term() is a function. The term() method takes only one [parameter](https://www.mindstick.com/blog/450/parameter-class-in-c-sharp) which is an object of text type. The Type of $text should be specified before the variable.

```
<?php
class text {
}
function term(text $text) {   // text is a object type for $text and should always be written before $text
echo 'Hello World!!';
}
$obj = new text();;
term($obj);   // display Hello World
?>
```

In the above example, it clearly specifies the type of $text which should always be an [object type](https://www.mindstick.com/blog/398/difference-between-object-type-and-var-type). Now let’s see one more example which will give the error:

```
<?php
class text {
}
function term(foo $text) {echo 'Hello World!!';}
$obj = new text();; // display Fatal errorterm($obj);?>
```

For the above example it will display [fatal error](https://answers.mindstick.com/qa/94996/how-do-i-fix-a-fatal-error-in-windows-10) as there is no such foo type object is found in the code.

**Output:** **Fatal error:** Uncaught [TypeError](https://www.mindstick.com/forum/157755/calling-a-class-member-function-gives-typeerror-in-python-why): Argument 1 passed to term() must be an [instance](https://www.mindstick.com/forum/1739/object-reference-is-not-set-to-an-instance-of-an-object-error-in-my-code) of foo, instance of text given

##### Type Hinting in Array

In the below example text() is a class and term() is a function. The term() method takes only one parameter which is an array type. The Type of $text should be specified before the variable.

```
<?phpclass text {}
function term(array $text) {   // type should be array onlyecho 'Hello World!!';}$obj = new text();;term(array($obj));   // display Hello World
?>
```

In the above example, it clearly specifies the type of $text which should always be an array type. Now let’s see one more example which will give the error:

```
<?phpclass text {}
function term(array $text) {echo 'Hello World!!';}
$obj = new text(); // display Fatal errorterm(123);
?>
```

For the above example it will display fatal error, argument must be array type but [integer](https://answers.mindstick.com/qa/113667/write-code-for-roman-to-integer) given.

**Output:** Fatal error: Uncaught TypeError: Argument 1 passed to term() must be of the type array, integer given\

---

Original Source: https://www.mindstick.com/blog/11164/type-hinting-in-php

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
