Skip to end of metadata
Go to start of metadata

TSPHP introduces type casting for class-/interface types next to type casting for primitive types.

Not supported

Please be aware, that object has a different meaning in TSPHP. The object cast:

\stdClass $a = (object) $b;

is not supported by TSPHP and results in a compiler error.

Following an example of an explicit cast:

class A{}
class B extends A{}
A $a = new B();
B $b = (B) $a;

Sub-types can be assigned to a parent-type without an explicit cast. In the example above this was done for

A $a = new B();

Please consider, that type juggling is not performed the same way as in PHP for scalar types. TSPHP adds some syntactic salt to its language definition compared to PHP. It is no longer possible to assign a scalar type to another one if data could get lost (e.g. assigning a string to int). One needs to use an explicit cast first (unless the cast modifier was used in the definition, then the explicit cast is turned into an implicit one).

Cast Assign Operator =() 

TSPHP introduces a new operator: =()

This is a shortcut for cast the right side of the = to the type of the left side. Following some pair of examples, once with =() and once without.

float $b = 1.2;
int $a =() $b;

float $b = 1.2;
int $a = (int) ($b);
float $b = 1.2;
float $c = 3.2;
int $d;
int $a =() ($d =() $b + 2 * $c) / 2.3;

float $b = 1.2;
float $c = 3.2;
int $a = (int) ($d = ((int) $b + 2 * $c)) / 2.3);
  • No labels