3

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!

centurian
  • 1,168
  • 13
  • 25
  • 1
    That's because namespaces aren't used for functions, they're used for classes. :) – Seer Mar 14 '13 at 20:04
  • @Seer Bzzzzzt. Wrong. – deceze Mar 14 '13 at 20:06
  • Try/Catch doesn't work because what @Seer said. And: http://stackoverflow.com/questions/1900208/php-custom-error-handler-handling-parse-fatal-errors – apelsinapa Mar 14 '13 at 20:10
  • @Seer *"Although any valid PHP code can be contained within a namespace, only four types of code are affected by namespaces: classes, interfaces, functions and constants."* http://www.php.net/manual/en/language.namespaces.definition.php – deceze Mar 14 '13 at 20:19
  • Well, you learn something new every day, <3 SO – Seer Mar 14 '13 at 20:50

1 Answers1

4

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.

deceze
  • 510,633
  • 85
  • 743
  • 889
  • Thanks 'deceze' for your answer: still find it very strange how these 'constructs' as you call them are implemented by the php team! – centurian Mar 15 '13 at 07:51
  • (Also, I just visited your project Kunststube-Router which I found very interesting as I'm trying to do the same for a project I'm working on-The Front Controller Pattern. Make a contact at your site so one can contact with!) – centurian Mar 15 '13 at 08:03
  • *Language constructs* are part of the language itself, not a pre-defined function. Think about it, a regular function cannot `unset` a variable. There's simply no mechanism for a regular function to do so. `unset` is defined by PHP as a reserved keyword and is handled by the PHP interpreter itself. Look through the PHP source code if you want to know the implementation details. – deceze Mar 15 '13 at 08:08
  • (And there are various contact details to be found in various places.) – deceze Mar 15 '13 at 08:09