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!

5
  • 1
    That's because namespaces aren't used for functions, they're used for classes. :) Commented Mar 14, 2013 at 20:04
  • @Seer Bzzzzzt. Wrong. Commented Mar 14, 2013 at 20:06
  • Try/Catch doesn't work because what @Seer said. And: stackoverflow.com/questions/1900208/… Commented Mar 14, 2013 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." php.net/manual/en/language.namespaces.definition.php Commented Mar 14, 2013 at 20:19
  • Well, you learn something new every day, <3 SO Commented Mar 14, 2013 at 20:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks 'deceze' for your answer: still find it very strange how these 'constructs' as you call them are implemented by the php team!
(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!)
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.
(And there are various contact details to be found in various places.)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.