Hudzilla.org - the homepage of Paul Hudson
Contents > Objects Wish List | Report Bug | About Me ]

6.9     Class type hints

This is NOT the latest copy of this book; click here for the latest version.

Although PHP remains a typeless language, which means you do not need to specify what types a variable is when it is declared or passed to a function, PHP 5 introduces class type hints, which allow you to specify what kind of class should be passed into a function. These are not required, and are also not checked until the script is actually run, so they aren't strict by any means. Furthermore, they only work for classes right now - you can't specify, for example, that a parameter should be an integer or a string. Having said that, I suspect future versions may introduce the ability to request that arrays are passed in - that's pure speculation, though!

Here is an example of a type hint in action:

<?php
    
class dog {
        public function
do_drool() {
            echo
"Sluuuuurp\n";
        }
    }

    class
cat { }

    function
drool(dog $some_dog) {
        
$some_dog->do_drool();
    }

    
$poppy = new cat();
    
drool($poppy);
?>

Note that the drool() function will accept one parameter, $some_dog, but that the parameter name is preceded by the class hint - I have specified that it should only accept a parameter of type "dog". In the example, I have made $poppy a cat object, and so running that will give the following output:

Fatal error: Argument 1 must be an instance of dog in C:\home\classhint.php on line 12

Note that providing a class hint for a class type that does not exist will cause a fatal error. As you can see, class hints are essentially a way for you to skip having to use the instanceof keyword again and again to verify your functions have received the right kind of objects - using a class hint is essentially implicitly using instanceof, without the extra code. You should make use of class hints regularly as they are a very simple way to make your code regulate itself, and helps solve bugs faster.

Author's Note: As with the instanceof keyword, you can specify an interface as the class hint and only classes that that interface will be allowed through.





<< 6.8 Object type information: instanceof and is_subclass_of()   6.10 Constructors and destructors >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
A PHP Luzr - 06 Sep 2008

Just a note: google for "php 5.2 optional typehinted paramters"

A PHP Luzr - 06 Sep 2008

It actually does since version 5.2 but is not promoted anywhere in the documentation. The syntax for this is:

class Boo{
// palce some code here
}

class Foo{
function moo(Boo $param = NULL){
// place some code here..
}
}

This way, the $param of function Foo::moo can bee either instance of Boo or null. However, you should check for the "nullness" of $param anyway...

Guna - 06 Sep 2008

It is good practice to use "instanceof" rather than "class type hinting".

class type hinting may be useful in development environment, but at production environment it advisable to avoid using class type hinting

A Fortran77 User ;o) - 06 Sep 2008

Note that when using class type hinting, using NULL as a parameter does not work.

In such a case you are better off by using 'instanceof' or 'is_subclass_off' and throw an exception if the result is false (e.g. SPL's InvalidArgumentException).



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is four plus zero?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow