We all know that the name of this class is
BattleManager. When we want to use it,
BattleManager. No matter what we do - static or non-static - if we want
to work with this class we call it by its name,
Why am I pointing out the painfully obvious? Because we're about to make this class name longer, but maybe not how you'd expect. We're going to use a namespace.
At first, why namespaces exist might not be obvious, so hold onto that question. Let's see how they work first.
Above any class, you can - if you want to - add a
namespace keyword followed by
some string. Like,
Battle or something more complicated like
A namespace is a string, and you can give it different parts by separating each with
\ - that's the slash that feels a little wrong when you type it - it's usually
an escape character.
To keep things simple, just set the namespace to
Battle for now:
|... lines 1 - 2|
|... lines 6 - 94|
As soon as we did that, we actually changed the name of this class: it is no longer
BattleManager. In fact, you can see that PhpStorm now highlights our code with
an "Undefined class BattleManager" error. Thanks to the namespace, the class is
|... lines 1 - 11|
|$battleTypes = Battle\BattleManager::getAllBattleTypesWithDescriptions();|
|... lines 13 - 140|
Refresh to prove it. Great!
So... that's really it! When you add a
namespace above a class, the full class
name becomes that namespace, a
\, and then class name. Every place we reference
this class name will now need to change - like inside of
Container. We'll do that
in a few minutes - we've got a few other things to do first.
Now that you know how namespaces work, you're probably wondering, why do these even exist? How does this help me in my coding? Well, the short answer is... it doesn't help you. In fact, namespaces weren't meant to help you - they were meant to help external library developers. So I guess, if you're one of those it does help.
In a nut shell, as you go further into development, you'll start to use a lot of 3rd-party, libraries written by other people. That's cool because those libraries will give us new classes to help solve problems.
The reason that namespaces exist is to avoid collisions in those external libraries.
Imagine we're using library A and library B, but that they both have a class called
Battle. Without namespaces, we'd be lost in space: we wouldn't be able to use
both libraries. But if each library has a unique namespace, we won't collide: they'll
simply be called something like
This means that namespaces do help us, but only indirectly. When we're working with namespaces it just makes our class names longer.
There is one other thing that you need to know with namespace: it's the
When you want to reference a class, it's perfectly valid to type out the entire
long class name right where you need to use it. But in practice, you won't see this
very often. Instead, people typically add a
use statement at the top of the file
that references the full class name:
|... lines 1 - 2|
|... lines 4 - 143|
As soon as you do, when you need to work with the class, you can once again write out only the short class name:
|... lines 1 - 2|
|... lines 4 - 14|
|$battleTypes = BattleManager::getAllBattleTypesWithDescriptions();|
|... lines 16 - 143|
And while you'll only have one
namespace per file, you'll have as many
as you need.
To be clear, the
use statement does not change how namespaces work: it's just
a shortcut. When PHP executes this file, it sees class
BattleManager and says:
BattleManager? Let me check all of the
usestatements at the top of this file.
PHP then looks to see if any of the
use statements end in the word
If it finds one, it basically copies the long class name and pastes it below
right before executing the file. What I just did manually is what PHP basically
does at run-time.
use statements are just this nice, extra feature. And technically, you could
avoid using them and instead write-out full class names right where you need
Ok! We're going to do a lot more with namespaces. But first, we need to turn to a very related topic called autoloading.
If you still have questions about namespaces - check out our short course PHP Namespaces in 120 Seconds or just leave a comment.