0

Seeing this line in PHP from password_compat, I am not sure what it does:

namespace {
  //...
  }

Is it similar to wrapping some code in an anonymous function in javascript? What's it purpose?

Note: I know how to normally use namespaces, I just don't understand this, since it looks like a namespace but without any name in it and, for me so far, without any purpose.

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90

2 Answers2

1

It declares the code to be in the global namespace. The purpose of this is that later on in the file there are two functions implemented in a private namespace, and PHP requires that if any namespace is used in the file the first keyword in the file must be namespace. So to mix global and namespaced code in the same file, this is how it needs to look.

See https://github.com/ircmaxell/password_compat/commit/88911e6abebb324cca88f546f04d6e71ce778bd3 for the particular commit.

deceze
  • 510,633
  • 85
  • 743
  • 889
0

That definition is to ensure that the file containing it loads in the global namespace. Have a look at this answer

namespace // empty namespace means global
{
    // Ensure everything you put here belongs the global namespace
}

But, technically speaking, it is exactly the same as not declaring any namespace at all. In both cases everything in it will belong to the global namespace.

Community
  • 1
  • 1
Alex Gidan
  • 2,619
  • 17
  • 29