Let's talk about the concepts related to namespaces in PHP.

Source: Internet
Author: User

Let's talk about the concepts related to namespaces in PHP.

Introduction to namespaces in PHP

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 (http://www.lai18.com/content/368824.html ).

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.



Additional reading

Summary of PHP basic knowledge 
Summary of the basic knowledge of PHP, for beginners or experts are worthy of reference and consolidation.

1PHP Method for checking whether the library or function is available

2. Introduction to PHP5 Session Lifecycle

3PHP page and Mysql database UTF8 encoding problem summary

4WAMP5 configuration problems

5 reveal why PHP is favored by Web developers

How to calculate and compare floating point numbers in 6PHP and solve the inaccuracy of Integer

7php method for obtaining client computer screen parameters

8. Get started with the namespace concept in PHP

For versions earlier than 9php5. 4, json does not support the solution that does not escape Chinese content

The 12 most frequently asked questions during 10PHP interviews

Example of 11PHP function extension_loaded () Usage

12 questions about precision loss of PHP floating point numbers

Using Regular Expressions in 13PHP to extract Chinese notes

Use break in 14PHP to jump out of multiple loop code instances

15 PHP implements directory traversal and deletes the specified content in the specified file

Introduction to namespaces in 16PHP

1750 tips for improving PHP Execution efficiency [reprinted]

Sort () of the 18php Array Function Sequence in ascending order of the array element values

Various Chinese encoding conversion classes implemented by 19PHP

20PHP uses mb_substr () to solve the problem of garbled characters in Chinese string truncation.

Example of get_object_vars () method usage in 21php

22PHP uses FormData object in combination with HTML5 to submit forms and upload images

23php uses parse_url and parse_str to parse the URL

How to implement encryption in 24PHP

25PHP magic variables and magic Functions

Explanation of return usage in 26PHP

Comparison and Analysis of 27Java and PHP in Web Development

Summary of several methods for 28PHP to obtain POST data

29 10 puzzles PHP beginners

Summary of 30PHP magic methods

31PHP: 40 + development tool recommendations

Use header in 32php to set content-type and content-type of common file types

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Related Article

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.