The concept of a namespace exists in several computer languages, but of those, today we start on a discussion of its encapsulation role with PHP.
A great source of information for information about PHP namespaces is this great link from the “home of PHP” … we always find.
Being as you can do without “namespaces” programming with PHP, let’s hone in on a couple of reasons you may want to make use of them (even so), taken from that aforesaid mentioned link …
- Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
- Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.
From that same link we modify some of its code to show you some concepts regarding this, having a named “namespace” section code establish its own “strlen($strvar)” function (negating the “\strlen($strvar)” call result).
We have this named “namespace” code set up its own MYCONST constant, and you can see how this works as well.
We don’t use it here, but also, in relation to named “namespace” code, you will want to research from the “home of PHP” the aliasing use of the PHP use keyword.
If this was interesting you may be interested in this too.