I really don’t know how to give an example without being detailed, but I’ll attempt this anyway:
Remember, a namespace is a hierarchical structure to help isolate code. It uses a folder paradigm to help with organization. It may or may not reflect the actual file structure for the php code (FIG standards suggest doing so, however. See the FIG PSR-4 standard for reference: https://www.php-fig.org/psr/psr-4/).
In this example I’m assuming the classes are loading automatically. That’s a completely different topic that I won’t cover here. (web search “php autoloading classes” for details.)
Consider the following namespace structure:
Vendor (the “root” namespace)
- Log
- User
- Elements
- Processes
- Login
- Menu
- ProcessData
- GetReport
Elements and Processes are sub-namespaces, like a sub-directory in a filing system. Log and User are classes in the \Vendor root namespace. Button, Checkbox, and Textbox are classes in the Elements sub-namespace, and Login, Menu, ProcessData and GetReport are classes in the Processes sub-namespace.
Let’s say I’m creating the Menu class in the Processes sub-namespace. I will put
namespace Vendor\Processes;
at the beginning of my Menu class php file showing that Menu is a part of the Vendor\Processes namespace, as the namespace command declares what code name space I’m defaulting to. If I failed to put the namespace command above in my Menu class php file, then Menu would automatically be in the Global namespace, denoted by a single backslash “\”.
As a member of the \Vendor\Processes namespace, the Menu class would have default access to Login, ProcessData, and GetReport. Meaning, if I needed to instantiate them I could simple use:
$process = new Login();
without qualifying the namespace structure because Login and Menu share the same namespace. If, however, I wanted to instantiate the Button class from Elements, I would need to qualify the Elements class so that php can reference the appropriate class. This is achieved by using
$button = new \Vendor\Elements\Button();
OR
use \Vendor\Elements\Button; // This is placed at the beginning of the file, typically under the namespace statement
$button = new Button(); // when I need to instantiate the new class
The “use” is like an include-in-search-path statement for name spaces. You can have several “use” statements each referring to a different name path. Also, the path does not have to include the class, it can just be a portion of the path (I’ll give an example in a moment). However, if you do this, the last part of the namespace path MUST be referenced when instantiating. This is a little confusing, so here is an example.
If I want to use the Button AND Textbox classes, there are 3 different ways I can accomplish it.
Method 1 - fully qualify the class name at instantiation:
$my_button = new \Vendor\Elements\Button();
// this is explained above
Method 2 - reference the parent path only:
use \Vendor\Elements;
then to instantiate my classes:
$button = new Elements\Button();
$textbox = new Elements\Textbox();
Notice I MUST included Elements again in the instantiation, or php will not find the class.
Method 2 - fully reference the classes in the “use” statement.
use \Vendor\Elements\Button;
use \Vendor\Elements\Textbox;
then later in my code instantiating the classes:
$my_button = new Button();
$my_textbox = new Textbox();
*coding tip: you can compress the 2 “use” statements above by including the final classes in curly brackets {} like this:
use \Vendor\Elements\{Button, Textbox};
All methods (fully qualify the class at instantiation, place a “use” statement referring to the parent namespace, or placing a “use” statement referring to the class itself) produce the same result. The Button and Textbox class are referenced appropriately. As to which method you use is really up to the programmer or programming team. There is no right or wrong way.