Fantastic question. It applies to both IDE documentation and PHP 5 Type Hinting.
You have to remember that in OO polymorphism is your friend.
If you create a base class and extend them, your type hint will be base class... all extended class will work. See example below.
//
$test = new DUITest();
// Calls below will work because of polymorphism
echo $test->test(new Molecule()) . '
';
echo $test->test(new Vodka()) . '
';
echo $test->test(new Driver()) . '
';
echo $test->test(new Car()) . '
';
// Will not work because different data type
echo $test->test(new Pig()) . '
';
echo $test->test(new Cop()) . '
';
echo $test->test('test') . '
';
echo $test->test(array()) . '
';
/**
* Class to test
*/
class DUITest {
public function __construct() {
;
}
/**
* Using type-hinting
*
* See below link for more information
* @link http://www.php.net/manual/en/language.oop5.typehinting.php
*
* @param Molecule|Car|Driver|Vodka $obj
*/
public function test(Molecule $obj) {
echo $obj;
}
}
/**
* Base Class
*/
class Molecule {
public function __construct() {}
/**
* Outputs name of class of current object
* @return
*/
public function __toString() {
return get_class($this);
}
}
class Car extends Molecule {}
class Driver extends Molecule {}
class Vodka extends Molecule {}
class Pig {}
class Cop extends Pig{}