PHP namespace concept parsing, php namespace parsing _ PHP Tutorial

Source: Internet
Author: User
PHP namespace concept parsing, php namespace parsing. PHP namespace concept analysis, php namespace resolution 1. what is the namespace in PHP? What is a namespace? In a broad sense, namespace is a way to encapsulate things. In a very PHP namespace concept analysis, php namespace analysis
1. what is the namespace in PHP?

What is a namespace? "In a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. For example, a directory in the operating system is used to group related files. for files in the directory, it plays the role of namespace. For example, the file foo.txt can exist in the/home/greg and/home/other directories at the same time, but there cannot be two foo.txt files in the same directory. In addition, when accessing the foo.txt file outside the/home/greg Directory, we must put the directory name and directory separator before the file name to get/home/greg/foo.txt. This principle is applied to the field of programming as a namespace concept ." -- Namespace overview

2. how can I understand PHP namespaces?

In essence, a namespace is a container in which classes, functions, and variables can be placed. they can access each other unconditionally in the same namespace. In addition to namespaces, you must reference or import other namespaces to call these items.

The namespace is the same as the file directory in shell. In the current directory, you can directly access all files with file names. to access files in other directories, you need to enter a relative or absolute path.
Reference method:

namespace foo; class Foo {            public function foo()                {                          return \top\namespace\bar\Bar::fuck();                  }             }

Import method:

namespace foo; use top\namespace\bar\Bar;  class Foo {        public function foo()             {        return Bar::fuck();              }           }

Importing is equivalent to copying the target class to the current namespace.

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

The namespace exists to solve the following two problems:

1. the user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.

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

4. some tips

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

2. if a class exists in a non-top-level namespace, it can only be called other classes under the same namespace as the current namespace without "reference" or "import". they belong to one. Any sub-namespace is another namespace, and another container does not have any special relationships except the relations between containers.

3. Laravel uses the classmap method for automatic loading. Although PHP has the advanced namespace feature, it is only a logical relationship and the require file still needs to be available. The correspondence between this class and the file exists in/vendor/composer/autoload_classmap.php. every time composer dump-autoload is compiled and generated.

What is the namespace in PHP? What is a namespace? In a broad sense, namespace is a way to encapsulate things. In very...

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.