🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php type hints
PHP Type Hints - PHP Tutorial
April 7, 2025 - To add a type hint to a parameter, you place a type in front of it like this: <?php function my_function(type $param1, type param2, ...) { // ...
🌐
PHP
php.net › manual › en › language.types.declarations.php
PHP: Type declarations - Manual
The performance of these methods is a mystery to the writer and so the responsibility of benchmarking them falls unto the reader. PHP 5.6 added the splat operator (...) which is used to unpack arrays to be used as function arguments. PHP 7.0 added scalar type hints.
🌐
Reddit
reddit.com › r/php › til how php type hinting works
r/PHP on Reddit: TIL how PHP type hinting works
March 24, 2023 -

Code example: https://onlinephp.io/c/0cd2c

Explainer (not mine): https://thephp.website/en/issue/php-type-system/

Wow, this is so cool. I should have looked into this years ago -- loose types have been my #1 complaint about PHP / the only reason I ever feel tempted to switch to Python.

This seems pretty fucking killer at reducing the passive rate of bugs introduced per new feature! (I learned about this concept from John Carmack's interview on the Lex Fridman Podcast.)

If you use PHP Type Hinting Declarations, are there any caveats / edge-cases I should be aware of?

I imagine I'll take a performance hit, so that's what I'll test next...

🌐
Honeybadger
honeybadger.io › blog › php-type-hinting
Reducing Errors With Type Hinting in PHP - Honeybadger Developer Blog
December 19, 2022 - Type-hinting means explicitly stating the expected type of declarations in your code. This allows you to enforce specific types in your code. PHP allows you to type-hint function parameters, return values, and class properties to write more ...
🌐
DEV Community
dev.to › the_realteckyguy › mastering-phps-type-hints-a-comprehensive-guide-to-writing-robust-code-46hp
Mastering PHP's Type Hints: A Comprehensive Guide to Writing Robust Code - DEV Community
May 4, 2025 - Type hints in PHP allow developers to explicitly declare the expected data type of a function’s parameters, return values, or class properties. Introduced in PHP 5 for objects and arrays, type hints were expanded in PHP 7 to include scalar types (e.g., int, float, string, bool) and additional features like return types and strict typing.
🌐
PHPpot
phppot.com › php › php-type-hinting
PHP Type Hinting: Function, Class, Return, Union, and Strict Types - PHPpot
July 1, 2026 - PHP type hinting lets you declare the expected type of a value in function parameters, return values, class properties, and more.
🌐
mlocati
mlocati.github.io › articles › php-type-hinting.html
Type hint in PHP function parameters and return values, properties and constants | mlocati - Michele Locati
November 23, 2023 - All the scalar, compound and special types used in function (and method) parameter and results, for any PHP version.
🌐
SitePoint
sitepoint.com › blog › php › type hinting in php
Type Hinting in PHP — SitePoint
November 6, 2024 - Since PHP 5 you can use type hinting to specify the expected data type of an argument in a function declaration. When you call the function, PHP will check whether or not the arguments are of the specified type.
🌐
Medium
articles.peterfox.me › mastering-php-type-hinting-techniques-22036df1f3ef
Type Hinting techniques of PHP | Medium
September 27, 2024 - Type hints are going to affect the signatures of any classes they use them with as extending a class with type hints already established will mean they can’t be overridden. An example of a class that makes full use of types would be: <?php class Foo { public function bar(array $strings): \Closure { return function (string $string) use ($strings): bool { return in_array($string, $strings); }; } }
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › php › explain-type-hinting-in-php
Explain type hinting in PHP - GeeksforGeeks
July 23, 2025 - Type hinting is a concept that provides hints to function for the expected data type of arguments.
🌐
DEV Community
dev.to › eelcoverbrugge › php-type-hinting-17n6
PHP Type Hinting - DEV Community
February 21, 2022 - Type hinting are type declarations to function arguments, return values and class properties. PHP ensures the value is of the same type you declared or throws an TypeError.
Top answer
1 of 7
40

Type hinting isn't required, but it can allow you to catch certain types of mistakes. For example, you might have a function or method which requires an integer. PHP will happily convert "number looking strings" into integers, and this can cause hard to debug behaviour. If you specify in your code that you specifically need an integer, this can prevent those kinds of bugs in the first place. Many programmers consider protecting their code in this way to be a best practice.

As a concrete example of this in action, let's look at an updated version of your index.php file:

index.php

<?php
include 'Song.php';
include 'Test.php';

$song_object = new Song;
$test_object = new Test;

$song_object->title = "Beat it!";
$song_object->lyrics = "It doesn't matter who's wrong or right... just beat it!";

$test_object->title = "Test it!";
$test_object->lyrics = "It doesn't matter who's wrong or right... just test it!";


function sing(Song $song)
{
    echo "Singing the song called " . $song->title;
    echo "<p>" . $song->lyrics . "</p>";
}

sing($song_object);
sing($test_object);

As well as the new Test.php file I added:

Test.php

<?php

class Test
{
    public $title;
    public $lyrics;
}

When I run index.php now, I get the following error:

Output:

Singing the song called Beat it!<p>It doesn't matter who's wrong or right...
just beat it!</p>PHP Catchable fatal error:  Argument 1 passed to sing() must
be an instance of Song, instance of Test given, called in test/index.php on
line 22 and defined in test/index.php on line 15

Catchable fatal error: Argument 1 passed to sing() must be an instance of
Song, instance of Test given, called in test/index.php on line 22 and defined
in test/index.php on line 15

This is PHP letting me know that I tried to use the wrong type of class when I called the sing() function.

This is useful because, even though the above example worked, the Test class could be different than the Song class. This could lead to hard to debug errors later on. Using hinting in this way gives developers a way to prevent type errors before they cause issues. This is especially useful in a language like PHP which is often eager to auto-convert between types.

2 of 7
9

Type hinting is a natural process. At first it may seem like extra work but it is very helpful as your project grows in PHP. It allows for better readability and makes error control and strict programming conventions easier to apply.

Initially, you must implement the ‘contract’, where the contract is a php interface which can ‘lock’ constants and the key public methods and their arguments, as such:

interface SongInterface {
    //...
}

class Song implements SongInterface
{
    public $title;
    public $lyrics;
    //...
}

Then continue with the actual execution part:

$song = (object) new Song;

$song->title = (string) "Beat it!";
$song->lyrics = (string) "It doesn't matter who's wrong or right... just beat it!";


function sing(SongInterface $song): string
{
    $html = (string)  "Singing the song called " . $song->title
    . "<p>" . $song->lyrics . "</p>";

    return htmlentities($html, ENT_QUOTES, 'utf-8');
}

echo sing($song);

Using an interface, you only define the functions and then implement it in a song class. This way you can always inject another song class (with newer functionalities and changes) without breaking your application. Check out oop interfaces: http://php.net/manual/en/language.oop5.interfaces.php

Since php7, you can also define the return type of a function. https://wiki.php.net/rfc/return_types

🌐
PHP
durak.org › sean › pubs › software › php-5.4.6 › language.oop5.typehinting.html
Type Hinting - PHP 5.4.6 Documentation
PHP 5 introduces type hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype), interfaces, arrays (since PHP 5.1) or callable (since PHP 5.4).
🌐
Tutorialspoint
tutorialspoint.com › php › php_type_hints.htm
PHP - Type Hints
PHP allows you to type-hint function arguments, return values, and class properties.
🌐
DEV Community
dev.to › xwero › php-features-type-hinting-2i2l
Php features: type hinting - DEV Community
May 5, 2024 - Before php 7.4 you could only add type hinting to functions, from php 7.4 you can add type hinting to class properties.
🌐
Kevin Quillen
kevinquillen.com › type-hints-and-return-types-php
Type Hints and Return Types in PHP | kevinquillen.com
June 16, 2019 - I decided to enable a new feature of PHP 7 to allow type hinting.
🌐
FlatCoding
flatcoding.com › home › php type hints: ensuring data integrity
PHP Type Hints: Ensuring Data Integrity - FlatCoding
June 29, 2025 - PHP-type hinting is something that keeps your code in check and helps your functions receive just the right types of data. Think about this: trying to follow a recipe, but someone hands you paint instead of milk—it just won’t work! Type hinting makes sure that your PHP code doesn’t get ...
🌐
Medium
medium.com › @binumathew1988 › mastering-php-type-hinting-techniques-with-examples-f8cb9f36a3f3
Mastering PHP: Type Hinting Techniques with Examples | by Binu Mathew | Medium
October 4, 2024 - Type hinting is a mechanism in PHP that allows you to specify the expected data type of arguments passed to functions or methods. It was introduced to improve code quality, making it easier for developers to catch bugs at an earlier stage.
🌐
PHP Classes
phpclasses.org › blog › post › 1047-php-8-type-hinting.html
What is New in PHP Type Hinting Support in PHP 8 - PHP Classes
January 26, 2021 - Type hinting means that a certain value type can be assigned to a function argument, a class variable or the return value of a function or class method. PHP does not yet provide a type hinting support for global and local variables, not even ...