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.
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.
Weakly-typed languages make conversions between unrelated types implicitly; whereas, strongly-typed languages don't allow implicit conversions between unrelated types.
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.
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.
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).
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With