Tom J Nowell
Tom J Nowell

Reputation: 9981

What was the rationale for adding a second namespacing operator to PHP?

In PHP there are two namespacing operators:

\ and ::

:: is used for internal namespacing, e.g:

namespace example;
class Foo {
    public static $bar = 'hello';
}

I can access $bar and other members of the Foo class via: Foo::$bar

The full namespaced name of Foo however is not example::Foo, it is example\Foo, and the full namespaced name of $bar would be example\Foo::$bar

What was the reason or rationale for using the two operators rather than sticking with one consistent operator that already existed?

Upvotes: 2

Views: 110

Answers (2)

Charles
Charles

Reputation: 51411

You should take a peek at the namespace separator RFC, which calls out the actual arguments used in favor of and against the backslash.

The main reason why :: wasn't picked was due to the possible ambiguity it caused when doing scope resolution. Given that namespaces can be aliased (use \Foo\Bar as Baz;), all sorts of hilarious hijinks could be caused.

Upvotes: 4

Hugo Delsing
Hugo Delsing

Reputation: 14163

The :: is the Scope Resolution Operator and is only ment to access static classes. It has nothing to do with the namespace. Also the namespacing has been added in PHP 5.3, so long after static classes.

I guess thats why they use two different operators (for two different goals)

Upvotes: 4

Related Questions