Differences between syntaxes of namespaces [closed]

2

I know two ways to declare a namespace:

// Comum
namespace Example\Class\Method;

class ExampleClass {...}

// Outro, similar a sintaxe C# (classe dentro do namespace)
namespace Example\Class\Method
{
    class ExampleClass {...}
}

Is there a difference between the two?

    
asked by anonymous 29.06.2016 / 17:10

2 answers

3

Every language has its way of dealing with things. If that was it, I do not even know if the question would make sense. In this case it is not only the syntax that is different, the semantics as well.

In PHP the definition holds for the whole file, until you find a new definition of namespace . It should always be done at the beginning of the file. At least in this syntax.

Question in more detail .

Documentation about definition . About using multiple .

Wallace says in comment that it may be different, but I have not seen a reference on the subject. I hate languages that document wrong or confusing. And I just trust the documentation. Using something that works but is not documented is the last thing a programmer should do. The keys also work in PHP.

Both C # and PHP can only have types as members of namespace .

In C # the members of a determining namespace are inside the keys that delimit it. Therefore it is possible to have more than one namespace per file.

Question in more detail .

Documentation .

The correct C # syntax:

namespace Example {
    class ExampleClass {...}
}

Do not put the class name, even less of the method in the name of namespace .

If it were to have a multi level name it would look like this:

namespace Nome.OutroNome.MaisUmNome

But note that deep down this separation is virtual. The name is one thing. There is not a namespace inside another. It is perfectly possible to have this namespace without having Nome . See question link above for more details.

    
29.06.2016 / 17:18
2

There is no difference between them.

In PHP, you accept namespaces declaration with keys and without keys.

In the case of using the keys, everything that is put in there will be part of the namespace you have declared with them.

namespace A {    
   class A {}
}

namespace B {    
    class A{}
}

In case of missing keys, the declared elements will be part of the namespace, until you declare another one.

namespace A;

class A {}

class B {}

namespace B;

class A{}

link

Already in C # it should always be with the keys.

    
29.06.2016 / 17:17