Current Location: Home> Latest Articles> Type Hinting in PHP7: How to Precisely Specify Function Parameter Types

Type Hinting in PHP7: How to Precisely Specify Function Parameter Types

M66 2025-07-10

Type Hinting Feature Introduced in PHP7

PHP7 introduces the Type Hinting feature, which allows developers to explicitly define parameter types in function declarations. This enables us to avoid type mismatches early in the development process, improving code readability and type safety.

How to Use Type Hinting for Parameter Types

Using Type Hinting is simple: just add the type declaration before the function parameters. Here's an example:

<span class="fun">function calculateSum(int $a, int $b): int {</span>
<span class="fun">    return $a + $b;</span>
<span class="fun">}</span>
<span class="fun">$result = calculateSum(5, 10);</span>
<span class="fun">echo $result; // Outputs 15</span>

In this example, we used the int type to declare parameters $a and $b. This means that the arguments passed to the calculateSum function must be integers, or else a type error will be triggered during compilation.

Handling Type Mismatch Errors

If we try to pass a parameter that does not match the declared type, PHP will throw an error during the compilation phase. For example:

<span class="fun">$result = calculateSum(5, "10");</span>

This will result in the following error:

Fatal error: Uncaught TypeError: Argument 2 passed to calculateSum() must be of the type int, string given

Supported Data Types

Type Hinting is not limited to scalar types (like integers, floats, and booleans). It can also be used with arrays, objects, and custom types. Here are a few examples:

<span class="fun">function processArray(array $arr): void {</span>
<span class="fun">    // Process array</span>
<span class="fun">}</span>
<span class="fun">function processObject(MyObject $obj): void {</span>
<span class="fun">    // Process object</span>
<span class="fun">}</span>
<span class="fun">class MyObject {</span>
<span class="fun">    // Class definition</span>
<span class="fun">}</span>
<span class="fun">$myArray = [1, 2, 3];</span>
<span class="fun">processArray($myArray); // Works fine</span>
<span class="fun">$myObject = new MyObject();</span>
<span class="fun">processObject($myObject); // Works fine</span>

Declaring Return Types

In addition to parameter types, Type Hinting allows us to specify the return type of a function. Here's an example:

<span class="fun">function calculateSum(int $a, int $b): int {</span>
<span class="fun">    return $a + $b;</span>
<span class="fun">}</span>
<span class="fun">$result = calculateSum(5, 10);</span>
<span class="fun">echo $result; // Outputs 15</span>

In this example, we explicitly declare the return type of the calculateSum function as int. If the function's actual return value doesn't match the declared type, PHP will throw an error:

<span class="fun">function calculateSum(int $a, int $b): int {</span>
<span class="fun">    return "15";</span>
<span class="fun">}</span>
<span class="fun">$result = calculateSum(5, 10);</span>

This will result in the following error:

Fatal error: Uncaught TypeError: Return value of calculateSum() must be of the type int, string returned

Conclusion

Type Hinting is an important feature introduced in PHP7. It helps detect and avoid type errors during the compilation phase, enhancing code readability and type safety. By explicitly declaring the types of function parameters and return values, developers can minimize potential bugs and improve code quality.