php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #55155 Cannot use non-bracketed namespaces in interactive shell on multiple lines
Submitted: 2011-07-07 05:32 UTC Modified: 2012-02-04 00:42 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: patrickallaert@php.net Assigned:
Status: Open Package: Scripting Engine problem
PHP Version: 5.3.6 OS: Linux
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2011-07-07 05:32 UTC] 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

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-07 12:06 UTC] johannes@php.net
-Status: Open +Status: Assigned -Assigned To: +Assigned To: johannes
 [2011-07-07 12:06 UTC] 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 14:26 UTC] 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 19:23 UTC] felipe@php.net
-Package: Compile Failure +Package: Scripting Engine problem
 [2012-02-04 00:42 UTC] johannes@php.net
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.
 [2012-02-04 00:42 UTC] johannes@php.net
-Status: Assigned +Status: Open -Type: Bug +Type: Documentation Problem -Assigned To: johannes +Assigned To:
 
PHP Copyright © 2001-2018 The PHP Group
All rights reserved.
Last updated: Mon Nov 19 18:01:29 2018 UTC