Skip to content

Namespace should not be nested #1165

@ve3

Description

@ve3

From PHP document ( https://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nested ), it said Nested namespaces are not allowed.

Here is same of PHP namespaces in 2 styles.

01 Simple style

namespace MyProject;
    const TASK_DATE = '';


namespace Another;
    echo TASK_DATE;

The echo line said Fatal error: Uncaught Error: Undefined constant "Another\TASK_DATE".

02 Bracket style

namespace MyProject {
    const TASK_DATE = '';
}

namespace Another {
    echo TASK_DATE;
}

The result is same error Fatal error: Uncaught Error: Undefined constant "Another\TASK_DATE".

=> This mean that simple style Another namespace is not nested under MyProject same as bracket style did.

BUT.. the php-parser parse these two differently!

Simple style result

{
  "kind": "program",
  "children": [
    {
      "children": [
        {
          "name": "Another",
          "withBrackets": false
        }
      ],
      "name": "MyProject",
      "withBrackets": false
    }
  ]
}

Bracket style result

{
  "kind": "program",
  "children": [
    {
      "name": "Another",
      "withBrackets": true
    },
    {
      "name": "MyProject",
      "withBrackets": true
    }
  ]
}

Since it is not allowed to nested namespace (from PHP document above) and the simple and bracket styles work the same, I think php parser should treat them the same as well. ??

JS Code I use to parse the PHP file.

// initialize a new parser instance
const parser = new engine({
  // some options :
  parser: {
    extractDoc: true,
    locations: true,
    php8: true,
  },
  ast: {
    withPositions: true,
  },
  lexer: {
    all_tokens: true,
  },
});

const phpFile = fs.readFileSync("./example.php");


// Log out results
console.log("File parse:", JSON.stringify(parser.parseCode(phpFile), null, 2));

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions