Skip to end of metadata
Go to start of metadata

The changes due to type safety are not listed in the following changes. Those changes cover statements which are not related to type safety (at least not directly) but will not be supported by TSPHP.

Multiple default cases in switch statements

It is perfectly legal to define more than one default case statement for a switch statement in PHP (this might well change in the future, see reported bug and the corresponding RFC Probably you are thinking now "whaaat? and which default case is used? the first, the last, all of them?". Do not worry about it, TSPHP does not support this "feature" since it hampers the readability of the code pretty much. If you are still curious which default statement is executed then have a look at the following PHP code.

PHP code
switch($a) {
    $b += 1;
    $b += 2; 
echo $b; //output would be 2
Please be aware, that TSPHP can not detect normal double cases because one can use an expression as a case label. TSPHP does not guarantee that always the first case is selected even thought it seems like PHP 5.4 behaves like this.

Use statement outside of a namespace

It is valid to write a use statement outside of a namespace in PHP even though the statement has no effect.

The following example causes a compiler error.

Would cause a compiler error
use \Exception; //use outside of namespace

namespace a; 

int $a = 1;

Pseudo optional parameters

It is valid to define an optional parameter followed by a non-optional parameter in PHP. Have a look at the following example:

PHP code
function foo($a=1,$b){}

It is not possible to call the function foo without defining a value for $b and therefore $a is a pseudo optional parameter. TSPHP does not support this construct and thus optional parameters have always to be at the end of the parameter list.

Have a look at the following valid and reasonable statement in PHP (thanks to Johannes Schlüter for the hint).

PHP code
function foo(Bar $a=null,$b){}

This construct is not valid in TSPHP. One can use the ? operator to specify that the parameter can be null. Please have a look at Function parameters  for more information.

Using a name of a keyword as identifier

PHP allows the use of the name of some keywords as identifiers (somehow they are not treated as keywords). Have a look at those (confusing) examples, which are all valid in PHP.

PHP Code
const this=1;
class object{
	const self =2, parent =3;
echo object::self + this + object::parent;
define("NULL","hello world");
var_dump(null); // null
var_dump(NULL); // hello world
if($a===NULL){ //$a !== "hello world"
	echo "aye";
	echo "whoops!";

One would probably never write something like this. In TSPHP it is just not possible.

Type aliases

TSPHP does not support the type aliases boolean, integer, double and real. They can not be used as type for a variable, type of a return value etc. nor in a cast. The following examples would all cause a compiler error.

Would cause a compiler error
boolean $a = true; //unknown type boolean
integer $b = 1;    //unknown type integer
double $c = 1.2;   //unknown type double
real $d = 2.5;     //unknown type double

bool $e = (boolean) $f;  //unknown type boolean
int $g = (integer) $h;   //unknown type integer
float $i = (double) $j;  //unknown type double
float $k = (real) $l;    //unknown type real

function void setStatus(boolean $status){}       //unknown type boolean
function void setQuantity(integer $quantity){}   //unknown type integer
function void setPrice(double $price){}          //unknown type double
function void setPrice(real $price){}            //unknown type real


Case sensitive class/method/function names

In contrast to PHP, class names as well as method and function names are case insensitive in terms of declaration. That means, it is not allowed to define two (or more) functions with the same name in a case insensitive manner. Thus the following would cause a compiler error:

Would cause a compiler error
function void foo(){}
function void Foo(){} //Foo was already defined in line 1 as foo

However, if one wants to call the functoin, then he has to use the case sensitive name. Thus the following would cause a compiler error as well:

Would cause a compiler error
function void foo(){}
Foo(); //function Foo does not exists.

The same applies for class and method names. It is not allowed to have two (or more) classes with the same name in a case insensitive manner per namespace and a class itself cannot contain two or more methods with the same name in a case insensitive manner.

We could say, the names are case sensitive but to protect the names, in order to avoid ambiguous names such as foo and Foo, the names are case insensitive during the definition check of the compiler.



  • No labels