tags:

views:

204

answers:

4

Hi

Is it normal to get the error "Default value for parameters with a class type hint can only be NULL" for a method in an interface defined as

public function nullify(bool $force=FALSE);

?

I need it to be bool, not an object, and FALSE by default.

+2  A: 

PHP 5 type hinting is limited to a specific class (or one of its subclasses), or an array. You cannot specify any other scalar types.

ctshryock
+6  A: 

There is no type hinting for boolean parameters in php (yet). You can only specify a class name or array. Therefore function foo(bool $b) means: "the parameter $b must be an instance of the class 'bool' or null".

http://docs.net/language.oop5.typehinting:

Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1).
VolkerK
+2  A: 

As already stated, type hinting only works for arrays and object. Your function can be written like this:

public function nullify($force=FALSE){
  $force=(bool)$force;
  //further stuff
}
dnagirl
Or raise an user_error or throw an exception if false===is_bool($force) to get closer to the "catchable fatal error" php raises for unsuitable parameters.
VolkerK
A: 

It's also possible to type hint with interfaces, beside arrays and objects (I suppose internally interfaces are also classes, but with no implementation).

Sorry to everyone, but I'll post this to wiki.

Flavius