Skip to end of metadata
Go to start of metadata

Please have a look at PHP's language reference. PHP calls fields properties.

TSPHP needs a declaration of a type for each (static) field in contrast to PHP. The type has to be written on the left hand side of the identifier of the field. It is possible to declare several fields of the same type in one statement. TSPHP does not require an access modifier in contrast to PHP and if one omits the access modifier, then public is automatically assigned to the corresponding field. Have a look at the following example which defines several fields with the type int.

class Foo
{ 
    int $a; 			        //declaration without assignment - access modifier is automatically public
    public int $a;              //declaration without assignment
	protected int $b=2;			//declaration with assignment
	private int $c,$d=3,$e;		//multiple declarations, with and without assignment
} 

Please consider, that a (static) field with a scalar type (boolintfloat and string) without assignment has a default value. Please have a look at the following table:

typedefault valueexample
boolfalse

private bool $a;
//...
bool $b = $this->a && true; // $b = false

bool!false 
bool?null 
bool!?null 
int0

private int $a;
//...
int $b = $this->a + 1; // $b = 1

int!false 
int?null 
int!?null 
float0.0

private float $a;
//...
float $b = $this->a + 1.0; // $b = 1.0

float!false 
float?null 
float!?null 
string""private string $a;
//...
string $b = $this->a."hello"; // $b = "hello"
string!false 
string?null 
string!?null 

 

  • No labels