Bug->Doc #55155 [Asn->Opn]: Cannot use non-bracketed namespaces in interactive shell on multiple lines
| From: | johannes@php.net | Date: | Sat, 04 Feb 2012 00:42:17 +0000 |
| Subject: | Bug->Doc #55155 [Asn->Opn]: Cannot use non-bracketed namespaces in interactive shell on multiple lines | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-7906@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=55155&edit=1
ID: 55155
Updated by: johannes@php.net
Reported by: patrickallaert@php.net
Summary: Cannot use non-bracketed namespaces in interactive
shell on multiple lines
-Status: Assigned
+Status: Open
-Type: Bug
+Type: Documentation Problem
Package: Scripting Engine problem
Operating System: Linux
PHP Version: 5.3.6
-Assigned To: johannes
+Assigned To:
Block user comment: N
Private report: N
New Comment:
Keeping the namespace has two issues
a) We can't get to global namespace
b) A typical use case I see is copy&pasting some code, if there's some namespace
declaration it some unexpected things might happen
I think the current behavior is the most clear one we can get, but this can probably be improved in
the docs:
Docs should mention the rule by which code is executed, this is defined in cli_is_valid_code(), http://lxr.php.net/xref/PHP_5_4/ext/readline/readline_cli.c#cli_is_valid_code
Basic logic is like this: Count brackets, parenthesis, comment and quote nesting, mind
heredoc/nowdoc, if all blocks and strings are closed and the code ends with } or ; it is seen as
"valid_end" and will be executed, else the next line will be read (and the default prompt
will be changed to one telling what is missing).
A chunk of code being executed behaves similar to code being included or eval()d.
Previous Comments:
------------------------------------------------------------------------
[2011-07-07 14:26:15] patrickallaert@php.net
> php > namespace a;
> php > class A {} // declares a\A
> php > class A {} // declares A
I would rather say that the second call to "class A{}" should remain as if we
were still in the namespace "a".
I would expect the following on the second declaration of class A:
PHP Fatal error: Cannot redeclare class a\A in php shell code on ...
------------------------------------------------------------------------
[2011-07-07 12:06:40] johannes@php.net
Whenever you press enter and the shell assumes the code might be valid (crrect amount of quotes,
braces etc.) it will be executed. So
php > namespace a;
and
php > class A {};
will be executed independently, similar to being in different files being included.
Maybe the logic for detecting whether it is executable code can be extended with this special case
.. on second thought that's no good idea this would cause cases like this:
php > namespace a;
php > class A {} // declares a\A
php > class A {} // declares A
... so we'd probably need a magic to set the current namespace. Not sure I really want that.
Becomes quite a hack.
Maybe it's best to document this limitation, but I'll try to think about a good thing to
do.
------------------------------------------------------------------------
[2011-07-07 05:32:46] patrickallaert@php.net
Description:
------------
When using PHP in interactive mode (php -a) namespaces must be used with the
bracketed form:
php > namespace a {class A {}}
php > namespace b {class A {}}
or on the same line:
php > namespace a; class A {};
php > namespace b; class A {};
Using:
php > namespace a;
php > class A {};
php > namespace b;
php > class A {};
should be semantically exactly the same but results in:
PHP Fatal error: Cannot redeclare class A in php shell code on line 1
Test script:
---------------
$ php -a
php > namespace a;
php > class A {};
php > namespace b;
php > class A {};
Expected result:
----------------
No fatal error.
Actual result:
--------------
PHP Fatal error: Cannot redeclare class A in php shell code on line 1
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=55155&edit=1