Home > headlines > Take you to play with PHP namespaces

Take you to play with PHP namespaces

无忌哥哥
Release: 2018-06-28 15:47:19
Original
2367 people have browsed it

* Namespace

* Let’s first understand an important concept: code reuse

* What is our ultimate goal in learning object-oriented programming? "Code reuse"

* 1. Thoroughly understand the code reuse mechanism?

* **So far, what we have learned about the implementation methods of code reuse

* 1. Function: Code reuse is the most important The simple way;

* 2. Classes and objects: encapsulate functions and the data they may use;

* 3. Traits and interfaces: a special class, the essence Is it still an encapsulation of functions and data;

* 4. Constants: Because constants cannot be modified once defined, they can be declared once and called anywhere without changing the value

* 2. Thoroughly understand what is the global?

* 1. The global and other refer to the current script, which cannot be cross-script, let alone the entire project

* 2. The global and the local are the same For twin brothers, they have a common parent: scope, which controls them

* 3. Thoroughly understand what members are in the php script?

* 1. Variables: include System preset variables and user-defined variables;

* 2. Constants: including system preset constants and user-defined constants;

* 3. Functions: including anonymous functions/function expressions /Self-executing function;

* 4. Classes and objects: including abstract classes, interfaces, traits;

* 3. Thoroughly understand what scope is?

** ****Scope is mainly for variables

*****Variables in PHP have three scopes: function scope, global scope, super global scope;

* 1. Global scope: declared outside the function, cannot be used directly inside the function

* Note: Global variables used in the function must be declared with global, or use a super-global array: $GLOBALS

* 2. Function scope: that is, local scope, including function parameters and variables declared within the function, which are automatically cleared when leaving the function;

* 3. Super global scope: In addition to being globally valid, You can also use it directly within a function without declaring

* 4. Why use namespace?

* 1. Classes, functions, and constants are all globally valid, and they can also be used directly when included in a function Use .

* 2. Therefore, classes, functions and constants with duplicate names are not allowed globally.

* 3. If you want to import a function library or class library from the outside, their function names and class names are likely to conflict with the current script

* 4. Therefore, you must have a set A mechanism for handling identifier naming conflicts for script members working in the global scope.

* 5. Namespace is such a solution. It not only allows users to give global members a shorter name, but also solves

* The introduction of third-party resources brings The problem of duplicate names.

* 6. Without using any namespace, all classes, constants, and functions are declared in the global space, using \ tables. Now we add

* before them. Except for the slash \, you will find that the execution result is also correct.

* When declaring classes, constants, and functions, you do not need to add \ in front of it. You only need to add \

## when calling. # * Namespace is very important. If you cannot fully master it, it will become the biggest obstacle to your future learning!!!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template