Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strong data typing bug

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.

like image 222
Flavius Avatar asked Oct 21 '09 12:10

Flavius


People also ask

What is strong data typing?

A strongly typed programming language is one in which each type of data, such as integers, characters, hexadecimals and packed decimals, is predefined as part of the programming language, and all constants or variables defined for a given program must be described with one of the data types.

What is strongly typed vs weakly typed?

Weakly-typed languages make conversions between unrelated types implicitly; whereas, strongly-typed languages don't allow implicit conversions between unrelated types.

Why is C# strongly typed?

The C# language is a strongly typed language: this means that any attempt to pass a wrong kind of parameter as an argument, or to assign a value to a variable that is not implicitly convertible, will generate a compilation error. This avoids many errors that only happen at runtime in other languages.

Is go strongly or weakly typed?

Go is a strongly, statically typed language. There are primitive types like int, byte, and string. There are also structs. Like any strongly typed language, the type system allows the compiler helps catch entire classes of bugs.


2 Answers

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.php.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).
like image 152
VolkerK Avatar answered Sep 28 '22 11:09

VolkerK


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
}
like image 24
dnagirl Avatar answered Sep 28 '22 11:09

dnagirl