What is a namespace?
The function names, class names and constant names declared in php cannot be repeated in the same run, otherwise a fatal error will occur. A common solution is to agree on a prefix. For example, during project development, it is not possible for both the controller and the data model in the User module to declare the same User class. You need to add their respective function prefixes in front of the class name. You can name the User class in the controller as the ActUser class, and the User class in the data model as the ModelUser class. Although this problem can be solved by adding a prefix, the name becomes too long, which means more code will be written during development. In versions after php5.3, many other high-level languages (such as Java, C#, etc.) have been added to use very mature functions ----- namespace. One of its clearest purposes is to solve the problem of duplicate names. Namespaces divide code into different areas, and the names of constants, functions, and classes in each area do not affect each other.
Note: Constants have changed since PHP5.3. You can use the const keyword to declare constants outside the class. Although const and define are both used to declare constants, in a namespace, define acts globally, while const acts on the current space. The constants mentioned here refer to constants declared using const.
The role and function of namespaces are very powerful. You no longer have to worry about duplicate names when writing plug-ins or general libraries. However, if the project progresses to a certain level and the problem of duplicate names needs to be solved by adding namespaces, the workload will not be less than refactoring names. Therefore, a naming convention should be well planned and specified from the beginning of the project.
Basic application of namespace
By default, all declarations of constants, classes and functions in PHP are placed in the global space . Versions after php5.3 have separate space declarations, and the same names in different spaces will not conflict. Independent namespaces are declared using the namespace keyword.
The code is as follows:
<?php //声明这段代码的命名空间 namespace abc; // 这里是代码 ?>
Note: namespace needs to be written at the top of the PHP script. It must be the first PHP instruction. Do not appear anything else before namespace. PHP code, HTML or spaces, etc.
Starting from the code namespace abc until the next namespac appears or the script ends, there is an independent space. Name this space abc.
If you namespace or define multiple namespaces for the same code it is not possible. If there are multiple namespaces used together, only the last namespace will be recognized, but you can define multiple namespaces in the same code. Different namespace codes are defined in the file.
The code is as follows:
<?php namespace project1; class user{ //此 user 属于project1 空间的类 } namespace project2; class user{ //此 user 属于project2 空间的类 } // 上面的替代语法,另一种声明方法 namespace project3{ //命名空间 project3 下的代码 } ?>
Although the above code is feasible, using their own user classes in different namespaces, it is recommended to Each independent file only defines one namespace, so the code readability is the best. In the same space, you can directly call any element in your own space, but you cannot directly call elements in other spaces between different spaces. You need to use the namespace syntax. The code is as follows:
<?php namespace project1; const test = "这是一个常量<br>"; function demo(){ echo "这是一个函数<br>"; } class user { function fun(){ echo "这是一个 user 类"; } } echo test; //在自己的命名空间中直接使用常量 demo(); //在自己的命名空间中直接调用函数 /*************** project2 ******************/ namespace project2; const test2 = "这也是一个常量<br>"; echo test2; \project1\demo(); //调用 project1 空间中的 demo() $user = new\project1\user(); //使用 project1 空间的类实例化对象 $user->fun(); ?>
In the above example, two spaces, project1 and project2, are declared. In your own space, you can directly call the elements declared in this space, and when calling the elements in project1 in project2, , using a syntax similar to a file path \space name\element name, the usage of classes, functions and constants is the same.
【Related tutorial recommendations】
1. "php.cn Dugu Jiujian (4)-php video tutorial"
2. Video tutorial :Namespace: Although we have the same name and the same gender, we belong to different time and space
3. Full set of tutorials on PHP programming from entry to master
The above is the detailed content of What is a namespace? Basic application sharing of php namespace. For more information, please follow other related articles on the PHP Chinese website!