Skip to end of metadata
Go to start of metadata
Link to PHP's language reference: http://php.net/manual/en/functions.arguments.php

Please have a look at PHP's language reference.

Not supported

Please be aware that variable-length argument lists are not supported by TSPHP

Not yet supported

Please be aware that passing by reference with the reference operator & is not yet supported by TSPHP

In contrast to PHP, one needs to define a type for each parameter and TSPHP supports scalar types for parameters.

As in PHP it is possible to define as many parameters as one likes.

TSPHP supports optional parameters as well. In contrast to PHP, it is necessary to define the optional parameters at the end of the parameter list (on the right side of mandatory parameters). Following some examples:

function void foo(int $b = 1, float $c = 3.0){} //only optional parameters
function void foo(string $a, int $b = 1, float $c = 3.0){} //mandatory and optional parameters mixed
 
//this would cause a compiler error - $b must be defined after $a
function void foo(int $b = 1, string $a,  float $c = 3.0){} 

However, TSPHP does not support pseudo optional parameters and the following two statements would both cause a compiler error.

Would cause a compiler error
function void foo(int $a=1, float $b){}
function void foo(A $a=null, float $b){}

The second statement seems to be reasonable because it indicates, that $a can be null. TSPHP supports this construct as well with the ? operator (see Function arguments). The use of the = operator in conjunction with parameters has only the purpose of defining optional parameters.

The ? operator

TSPHP introduces the new operator ?. The ? operator in conjunction with parameters can be used to specify, that a parameter can be null (is Nullable). Please have a look at the following function.

function void foo(Bar $b){}

If one would pass null as argument (regardless if null directly or the value of a variable is null) to this function, then a runtime error would occur. One has now the possibility to specify that null is a valid value by adding ? to the right of a type. For instance

function void foo(Bar? $b){}

If one is familiar with PHP then one will recognise, that this behaviour corresponds to =null in PHP. Well, almost. =null does two things at the same time. It defines that null is a valid value for $b and at the same time it defines $b as an optional parameter. TSPHP treat the = operator in conjunction with parameters only as specifier of optional parameters (have also a look at Pseudo optional parameters). This way it is possible to define statements as the above one: null is valid but one has to pass an argument.

One can still use =null to specify that a parameter is optional and his default value is null. Logically this can only be used, if the corresponding parameter has the ? operator. Have a look a the following examples:

function void foo(Bar  $bar, Foo? $foo){}    		 //null is only valid for $foo
function void foo(Bar? $bar, Foo? $foo=null){} 		 //null is valid for both parameters, but only foo is optional
function void foo(Bar? $bar=null, Foo? $foo=null){}  //null is valid for both parameters and both are optional

Please be aware, that the ? operator in conjunction with scalar types has an additional meaning (see Nullable). If one uses ? for a parameter with a scalar type, then one can pass null as argument for this parameter as one would do it for class-/interface-type. The main difference lies in the further usage of the parameter. Have a look at the following example which would cause a compiler error.

Would cause a compiler error
function void foo(int? $a=1, Bar? $b){
	Bar $b2 = $b; //that's absolutely fine
	int $a2 = $a; //that results in a compiler error
}

? in conjunction with scalar types is not merely used to define that null is a valid value for the corresponding parameter, but also defines a different type than the scalar type without ?.

The cast modifier

Have a look at cast modifier for an explanation how the cast keyword can be used in conjunction with functions (and methods).

  • No labels