The concept of namespace-related concepts in PHP _php skills

Source: Internet
Author: User
Tags autoload

1. What is the namespace in PHP?

What is a namespace? "In the broadest sense, namespaces are a way of encapsulating things." This abstract concept can be seen in many places. For example, in the operating system the directory is used to group related files, which play a role as namespaces for the files in the directory. For example, file Foo.txt can exist in both the directory/home/greg and/home/other, but there cannot be two foo.txt files in the same directory. In addition, when accessing foo.txt files outside of the directory/home/greg, we must get/home/greg/foo.txt the directory name and the directory delimiter before the filename. The principle applied to the domain of programming is the concept of namespaces. "--Namespace overview

2. How do I understand the PHP namespace?

In essence, a namespace is a container in which we can put classes, functions, and variables, which they can access unconditionally to each other within the same namespace. Outside of namespaces, you must reference or import other namespaces in order to invoke the items they contain.

Namespaces are the same as the concept of a file directory in a shell. In the current directory, you can access all files directly with file names, and if you need to access files in other directories, you need to enter a relative or absolute path.

Reference mode:

Copy Code code as follows:

namespace Foo;
Class Foo {
Public Function foo ()
{
return \top\namespace\bar\bar::fuck ();
}
}

Import Method:
Copy Code code as follows:

namespace Foo;
Use Top\namespace\bar\bar;
Class Foo {
Public Function foo ()
{return Bar::fuck ();
}
}

Importing is equivalent to copying a destination class into the current namespace.

3. What is the practical application of the PHP namespace?

namespaces exist to address the following two issues:

1. User-written code conflicts with the name of a class/function/constant or third party class/function/constant inside PHP.

2. Create an alias (or short) name for a long identifier name (usually defined to mitigate the first type of problem), improving the readability of the source code.

4. Some tips

1. Classes in the same space directly call each other directly, belong to one. For example, in the Pagecontroller class in Laravel, you can write code such as Page::all () to call Page model because both of them are under the top-level namespace.

2. If a class exists in a non-top-level namespace, it can only be used in calls to other classes under the current namespace without reference or import, they belong to one. Any child namespaces are another namespace, and the other container does not have any special relationships other than the relationships between the containers.

3. Laravel adopts Classmap mode for automatic loading (autoload), although PHP has the advanced feature of namespace, but this is only a logical relationship, require file still need to have. The corresponding relationship between this class and file exists/vendor/composer/autoload_classmap.php, each composer Dump-autoload will be recompiled and generated.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.