phpnamespacesstandardspsr-1

Namespace entries in PSR-1 standard?


I am looking for the correct way (if either is correct please let me know) to write namespaces in PHP classes that follow PSR-1 standard. I looked around but didn't see anything specific. Is the correct way to use

Namespace Foo\Bar;

or is it correct to use curly braces such as

Namespace Foo\Bar
{
    // ....
}

If both are correct, is there a particular reason to use one over the other? I realize that this portion might be opinion based though.


Solution

  • The bracketed namespace is the recommended method for defining multiple namespaces in a single file.

    If you only have one namespace in a file then use the non-bracketed version.

    PSR-1 recommends one class per file, so you should probably limit yourself to one namespace per file, too.