I was very pleased, that PHP 5 introduced
type hints, although they are not available for primitives like
string,
int,
boolean, etc. Still, I'd like to see the
object type hint introduced in any future version of PHP that allows me to specify, that a method or function only accepts an object, regardless of the type of the object:
class Processor {
public static function processObject(object $obj) {
echo get_class($obj) . "\n";
}
}
Currently you always have to specify a class or interface name, but I can't see, why this is needed. In Java, this is no problem, as they have a
common base class for all of their classes, which is not the case with PHP. So when trying to call this method with an object like this:
class Foo{}
Processor::processObject($foo);
you get the following error:
Catchable fatal error: Argument 1 passed to Processor::processObject() must be an instance of object, instance of Foo given, called in test.php on line 14 and defined in test.php on line 6
The
reflection extension lets us do a lot of funky stuff without knowing anything about the passed object, so I can't see a reason why this feature should not be implemented. If this already is possible in PHP, please let me know. If not, I hope that Marcus Börger is reading this entry and will implement this for PHP 6

. And yes, I'm well aware, that this might create some problems if anybody has a class named
Object, but please don't start another namespace flamewar (although namespace are one of my other wishes for PHP 6).
And while we are talking about it: What about a
ressource type hint, that will only accept ressources, that can be processed by functions like
fread() and
fclose()?