centurian
centurian

Reputation: 1194

called php functions from a namespace

It seems I found a error when calling some php functions from a namespace that I can't understand it:

<?php
namespace test;
$var = "foo/bar";
echo 'let\'s call \strpos($var, \'o\'):', \strpos($var, 'o');
try{
 echo '<br />let\'s call \unset($var):';
 \unset($var);  //error!
 unset($var);  //correct!
 echo '<br />let\'s call \isset($var):';
 \isset($var);  //error!
 isset($var);  //correct!
}catch(\Exception $e){
 echo 'We have error:', $e->getMessage();
}
?>

Php says: Parse error: syntax error, unexpected T_UNSET, expecting T_STRING in global_namespace.php on line 7 Not even try...catch works and the error is reported ONLY for global functions isset() and unset()!

I fond it very bizarre, at least!

Upvotes: 3

Views: 395

Answers (1)

deceze
deceze

Reputation: 522024

isset and unset aren't functions, they're language constructs. That means they're closer to operators like + and = than functions, hence do not play by the same rules. There's only one unset, you couldn't redefine it as a function if you wanted to.

Further, errors are not exceptions. You can't catch an error because it's not thrown. Even more so for syntax/parser errors, which happen before any code is even executed.

Upvotes: 4

Related Questions