Type Hinting in PHP

    Amanda Steigerwalt

    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. If not, the run-time will raise an error and execution will be halted.

    Valid types are class names for arguments that receive objects and array for those that receive arrays. Here’s an example:

    function enroll(Student $student, School $school) {
    echo "Enrolling " . $student->name . " in " . $school->name;

    By telling PHP exactly what kind of objects the enroll() method expects to receive, you can ensure that students are being enrolled in a school instead of a nunnery or a 401K. Likewise, you know that you won’t have any stubborn pachyderms showing up for the first day of third grade.

    What would happen if I tried to enroll myself into medicare?

    $me = new Student("Amanda");
    $medicare = new Program("Medicare");
    $enroll = enroll($me, $medicare);

    Although I am a student, the following error would occur:

    Catchable fatal error: Argument 2 passed to enroll() must be an instance of School, instance of Program given, called in typehint.php on line 32 and defined in typehint.php on line 6

    If null is used as a default value for an argument it will also be allowed. Here’s an example, this time with arrays:

    function startParty(array $guests, array $food = null) {
        // party stuff...
    $guests = array("Susan Foreman", "Sarah Jane Smith", "Rose Tyler", "Donna Noble");
    startParty($guests, null);

    There’ll be a party as long as there are guests, with or without food.

    Limitations of Hinting

    Any defined class can be a valid type hint, though PHP does not support type hinting for a generic object. What about everything else?

    Here is a peculiar example of the limitations of PHP’s type hinting:

    function stringTest(string $string) {
        echo $string;
    stringTest("definitely a string");
    Catchable fatal error: Argument 1 passed to stringTest() must be an instance of string, string given, called in typehint.php on line 42 and defined in typehint.php on line 39

    You’re not the first think “What is this madness? I gave you a string instance, and yet you complain it must be an instance of string!” It’s alright. It happens to the best of us. In fact, it can be quite a
    confusing error message at first glance.

    stringTest() is not looking for a string, it is looking for an instance of a string class. Scalar data types, such as strings or integer values, are not supported in PHP’s type hinting. But it’s okay! If you need to raise an error or throw an exception when an argument is not a scalar type (like a string or integer), you can do perform basic validation to serve this purpose using functions like is_string() or is_int().

    The Scalar Wars

    There has been a bit of controversy regarding the addition of scalar PHP type-hinting in PHP 5.4. Those who oppose the change argue that this support would go against the fundamental designs of PHP. PHP is considered to be a weak typed language. In essence, this means that PHP does not require you to declare data types. Variables still have data types associated with them but you can do radical things like adding a string to an integer without resulting in an error.

    In May of 2010 support for scalar type hinting was added to the PHP trunk. But because of community response this feature will not make its way into the 5.4 release.


    Type hinting is a technique introduced into PHP for object-oriented programming (specifically for identifying the type of a caught exception). I encourage you to read more about working with objects here.

    Image via Carlos E. Santa Maria / Shutterstock