yes good point,
however if I find loose types very useful when you declare variables, with functions I don't. It is very helpful to be able to force some type of return in interfaces/abstract classes... this allows to create frameworks where the end developer directly knows what he can do and what he cannot.
It also reduces the amount of code, because there is no need to make type checks for each method of some interface implementation, just check that the instance is of type <Interface> and you are sure that all return types will be correct:
- Interface PDOStatement_Interface
-
{
-
public function bool bindColumn ( mixed $column , mixed &$param [, public function int $type [, int $maxlen [, mixed $driverdata ]]] )
-
public function bool bindParam ( mixed $parameter , mixed &$variable [, int $data_type = PDO::PARAM_STR [, int $length [, mixed $driver_options ]]] )
-
public function bool bindValue ( mixed $parameter , mixed $value [, int $data_type = PDO::PARAM_STR ] )
-
public function bool closeCursor ( void )
-
public function int columnCount ( void )
-
//...
-
}
-
Class Foo
-
{
-
static public $adapterClassName = 'SomeAdapterClass';
-
static public function getPDOStatementAdapter()
-
{
-
$adapter = new self::$adapterClassName();
-
if ($adapter instanceof PDOStatement_Interface) {
-
throw new Exception('Your adapter does not implement interface
-
thus it will crash when i'll try to use methods that don\'t exist or that
-
don\'t return the value that i need');
-
}
-
return $adapter;
-
}
-
}
-
In my example if the interface can't check the return types, then when using the adapter methods, one should check that the return type is what it is expected, which could result in 30 methods, used in 50 different places => 30*50 = 1500 if statements:
-
//here we asume PHP has not type hinting
-
$res = Foo::getPDOStatementAdapter()->bindColumn(/*...*/);
-
if (!is_bool($res)) {
-
throw new Exception('Sorry the return type of your adapter is not supported!');
-
}
that's the main reason i like type hinting in methods.
but for those who don't need it the php developers would probably(almost 100% sure) allow two types of usage : the old fashion:
- publid function someMethod();
and the new one :
- public function string someMethod();