How to use namespaces in PHP at a glance
The concept of a namespace has long been present in many popular programming languages. Finally, starting with version 5.3, this feature has been introduced into the PHP language.
Why use namespaces ? Using a namespace will help avoid class naming conflicts and will also allow you to more transparently see the structure of the project. By default, if you do not specify a namespace, the class will exist in the global namespace, the same thing happened when PHP did not support this functionality. Now, it is enough to write something like the following before declaring the class, and the class will already exist in the specified namespace:
Where "my_namespace" is any name, names can be like addresses, backslash \ is used to separate path parts. Once declared, you can use the class like this:
$ object = new my_namespace \ Class ();
Alternatively, you can specify that a specific namespace will be used so that you do not have to explicitly specify it each time an object is instantiated. This is done using the following construction:
use my_namespace \ Class;
If you implement a class autoloader, then it will be enough to take part of the path "my_namespace \ Class" and glue it to the other part of the path where the classes are stored. This way, autoloading of classes can be easily implemented.
For more information on namespaces that will take a while to learn, see the official PHP website at this address .
- IT / Misc 08.07.21 How to make a free translation for a website without an API, translate documents in Google Translate
- IT / Misc 06.07.21 How to make a subscription button on a website, a subscriber base and automatic mailing
- Food / Misc 06.07.21 How to quickly cook delicious fried pies with potatoes and onions
- IT / Misc 04.07.21 Caching - create, load and reset. Where to store the cache, methods and types of caching
- IT / Database 03.07.21 Custom NoSQL - storing data in files and not only in a database. Storing settings, small data and caching files