Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type Hinting: Default Parameters

PHP 5 Type Hinting

PHP 5 introduces Type Hinting. 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). However, if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

The following excerpt from the above:

if NULL is used as the default parameter value, it will be allowed as an argument for any later call.

Does the above mean:

if default parameters are to used use with type hinting, it can have only have NULL as the default value.

i.e. the code in code1 is wrong and results in:

Fatal error: Default value for parameters with a class type hint can only be NULL

code1:

 function setName ( string $name = "happ") {   ...   } 

Where as code in code2 is right:

code2:

 function setName ( string $name = NULL) {   ...   } 

Why is this constraint assigned in php?

like image 489
ThinkingMonkey Avatar asked Dec 15 '11 16:12

ThinkingMonkey


People also ask

What is the point of type hinting?

Type hints improve IDEs and linters. They make it much easier to statically reason about your code. Type hints help you build and maintain a cleaner architecture. The act of writing type hints forces you to think about the types in your program.

Which of the following function types of function Cannot have default parameters?

Constructors cannot have default parameters.

What does type () do in Python?

Syntax of the Python type() function The type() function is used to get the type of an object. When a single argument is passed to the type() function, it returns the type of the object. Its value is the same as the object.

Does Python enforce type hints?

Unlike how types work in most other statically typed languages, type hints by themselves don't cause Python to enforce types. As the name says, type hints just suggest types. There are other tools, which you'll see later, that perform static type checking using type hints.


1 Answers

You can't typehint strings, you can only typehint objects and arrays, so this is incorrect:

function setName ( string $name = "happ") { 

(The reason you don't get a compile-time error here is because PHP is interpreting "string" as the name of a class.)

The wording in the docs means that if you do this:

function foo(Foo $arg) { 

Then the argument passed to foo() must be an instance of object Foo. But if you do this:

function foo(Foo $arg = null) { 

Then the argument passed to foo() can either be an instance of object Foo, or null. Note also that if you do this:

function foo(array $foo = array(1, 2, 3)) 

Then you can't call foo(null). If you want this functionality, you can do something like this:

function foo(array $foo = null) {     if ($foo === null) {         $foo = array(1, 2, 3);     } 

[Edit 1] As of PHP 5.4, you can typehint callable:

function foo(callable $callback) {     call_user_func($callback); } 

[Edit 2] As of PHP 7.0, you can typehint bool, float, int, and string. This makes the code in the question valid syntax. As of PHP 7.1, you can typehint iterable.

like image 94
Alex Howansky Avatar answered Oct 13 '22 18:10

Alex Howansky