Undestanding PHP Namespaces
I have hard time understanding PHP namespace. So, this post is things I learn from working with it.
Namespace keyword starts the file.
When you include the class that has namespace defined, you also need to type the keyword use and the namespace you want to use follows. Without using namespace, this would work. However, with namespace, the keyword use is required.
The main purpose of namspaces in PHP is to avoid name conflict.
Namespace is used in class. You cannot declare a namespace and in that file don’t declare a class.
In one file, you can have class definition and code (functions, variables) outside of that class. This is strange for me since I learn OOP first from Java. You cannot access such functions/properties from other files though (even if you include it). However, these functions, variables are accessible in the classes of the same file. Maybe it’s a way of code sharing without declaring a class with static functions/properties.