Skip to end of metadata
Go to start of metadata

Please have a look at PHP's language reference.

Not supported

Please be aware, that TSPHP does not support conditional functions and functions within functions

 

TSPHP treat functions a little bit different than PHP. First of all, TSPHP treats function names differently. Please have a look at  Case sensitive class/method/function names. Furthermore, one needs to define the type of the return value as well as a type for each parameter. The syntax of a function looks like the following:

function returnType identifier(type $parameter, cast type $parameter, type? $parameter /*, ... */, type $parameter = defaultValue ){}

Whereby returnType defines the return type. A return type can either be any primitive-/class- or interface-type in the case where a value is actually returned or "void" if no value is returned. Have a look at the two examples which follows.

function void setName(string $name){} //function without return value
function string getName(){} //function with return value of type string

type defines the type of the corresponding $parameter. 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.

As in PHP it is possible to define functions without parameters.

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