Differences between namespace syntax

Asked

Viewed 69 times

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 any difference between the two?

  • I’m not sure what you want to know and the syntax of C# is wrong. You want to know about the key and the semicolon?

  • Yes what I meant was with the class within the namespace as well as in the C#

2 answers

3


Each language has its own way of dealing with things. If that were the case, I don’t even know if the question would make sense. In this case it is not only the syntax that is different, the semantics also.

In PHP the definition is valid 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 with more details.

Documentation on the definition. About the multiple use.

Wallace says it might be different, but I didn’t see a reference to it. I hate languages that document wrong or confusingly. And I only trust the documentation (when it’s not wrong either). Using something that works but is not documented is the last thing a programmer should do.

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 by file.

Question with more details.

Documentation.

The correct syntax of C#:

namespace Example {
    class ExampleClass {...}
}

I put in the Github for future reference.

No class name, even less method name in the name of the namespace.

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

namespace Nome.OutroNome.MaisUmNome

But note that in the background this separation is virtual. The name is one thing. There is no one namespace within another. It is perfect to have this namespace without having the Nome. Vide question linked above for more details.

  • In PHP accept both ways :p

  • Can it be, is there a reference to it? But if it’s the way the question goes, I don’t think you can have more than one, at least it’s what’s in the documentation. Is there a bug in it? http://php.net/manualen/language.namespaces.definition.php

  • You can. As long as the script only contains definitions of classes and namespaces :p

  • But there’s documentation saying this?

  • I don’t even look at the documentation sometimes, just test it. There’s a lot that doesn’t speak in the PHP documentation. But I can check it out.

  • If I use only namespace {...} is global, what do you mean? I should ask another question about this?

  • @José where did you see this? Documentation? PHP or C#? I think it’s a case for another question yes.

  • @bigown Yes to documentation, http://php.net/manual/en/language.namespaces.definitionmultiple.php is just go down until you get to the contributing notes, the last part of the example #4 documentation

  • @Joseph briefly is a namepace even, as it has no name, is always available (after all it has no way to import something that has no name). We can say that everything that is not explicitly stated, is in this namespace.

Show 4 more comments

2

There’s no difference between them.

In PHP you accept namespace declaration with keys and no keys.

In the case of the use of the keys, everything that is placed there will be part of the namespace that you declared with them.

namespace A {    
   class A {}
}

namespace B {    
    class A{}
}

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

namespace A;

class A {}

class B {}

namespace B;

class A{}

https://ideone.com/lcqvu8

Already in C# should always be with the keys.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.