type hint method/function return value – PHP

  php

Q(Question):

Hi,
I was wondering if there is a way (from an interface or abstract class) to force all classes implementing some method to return a specific type, like in Java.

example :

//method can only return strings
public function string someMethodName();
//method can only return PDOStatement instances
public function PDOStatement prepare();

thank you for your help!

A(Answer):

Nope, not currently, although there is are RFCs on the matter. My views on this feature are undecided yet.

A(Answer):

thanks a lot markus!
well I don’t know if this will be very expensive in efficiency, but for coding, I find it easier to explicitly tell in an interface what a method should return, than in documentation or even in the piece of code (where the method is supposed to be called).
Anyways I’m not an advanced programmer but that’s how i feel about it.

A(Answer):

the big problem is, PHP is a loosely typed language. forcing an return type completely defeats that concept.

A(Answer):

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();

A(Answer):

more arguments against return types:
– how to implement (on the PHP side)? if you return a variable, it could have any type, and not every type can be converted into any other type. (it might not be a huge problem with String, but with object/interface)
– new coders will fail to grasp the concept
– you need a pre-compiler
– incompatibility with old scripts

A(Answer):

I don’t understand what you mean when you say : "- how to implement (on the PHP side)? if you return a variable, it could have any type, and not every type can be converted into any other type. (it might not be a huge problem with String, but with object/interface)"
why should you convert to another type than the one returned? the purpose of restricting the return type, is to make the script crash when the return type is not the one expected.

you are right on the second point, and it is important because php has a huge percentage of new coders. but probably the people at php will allow the old fashion use, that’s why I don’t think that (if ever implemented) it will be incompatible with old scripts.

for the pre-compiler you are probably right again, but wouldn’t it also be possible to make the return type check on execution? that would limit the return type checks to the methods that get executed, which isn’t as great as with a pre-compiler. Now i think that the greatest thing would be to use both :
-a runtime check and
-a pre-compile check
when in development, you could activate the pre-compile check with an ini_set(‘pre-compiler’, ‘on’).
Then when you go to production you disable it and the checks are only made when the methods get executed.

LEAVE A COMMENT