Marty Wallace
Marty Wallace

Reputation: 35744

PHP Namespace confusion - keyword v constant

I'm learning about php namespaces just now and am stumped as to what the difference is between the namespace keyword and the namespace constant:

__NAMESPACE__
namespace

All the examples on php.net seem to use the keyword like so:

namespace\MyClass or something similar. Which could easily have been done with the constant also.

Can someone explain the fundamental difference between them and when to use either over the other.

Upvotes: 1

Views: 90

Answers (1)

Dai
Dai

Reputation: 155360

The namespace keyword namespace is used to define a namespace in a file, similar to C#'s namespace keyword (but without the braces to define scope), or Java's package keyword.

In PHP, the __NAMESPACE__ constant returns a string value of the current namespace scope name. Try to avoid using it as it muddies your code up, but it's useful when using strings as typenames.

There's more documentation in the PHP manual: http://www.php.net/manual/en/language.namespaces.nsconstants.php

Upvotes: 1

Related Questions