Talk about namespace-related concepts in PHP

Source: Internet
Author: User
Tags autoload php basics

Talk about namespace-related concepts in PHP

1. What are the namespaces in PHP?

What is a namespace? "In a broad sense, namespaces are a way of encapsulating things. Such abstractions can be seen in very many places.

Like what. Folders in the operating system are used to group related files. For a file in a folder, it plays the role of a namespace. For a detailed example, file Foo.txt can exist in folders/home/greg and/home/other at the same time. However, there cannot be two foo.txt files in the same folder.

Other than that. When visiting the Foo.txt file outside of the folder/home/greg, we must place the folder name and the folder delimiter before the file name to get/home/greg/foo.txt. The application of this principle to the field of programming is the concept of namespaces. "--Namespace overview

2. How is the PHP namespace understood?

In essence, a namespace is a container in which we can put classes, functions, and variables. They have unconditional access to each other within the same namespace.

Outside of the namespace, you must reference or import additional namespaces. Ability to invoke these items that they include.



Namespaces are the same as the concept of a file folder in the shell. In the current folder can be directly to the file name access to all files, if you need to visit the files in other folders, you need to enter a relative path or absolute path.

Citation method:

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


How to import:

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

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

The namespace exists 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 within PHP.

2). Create an alias (or short) name for a very long identifier name (typically defined to alleviate 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, belonging to one. For example, the Pagecontroller class in Laravel can directly write Page::all () code to invoke the Page model. Because both of them are under the top-level namespace.

2. If a class exists in a non-top-level namespace, it is only possible to invoke other classes that are the same as the current namespace without "referencing" or "importing." They belong to a family. There is also a namespace for whatever sub-namespace. There is also a container that does not have any special relationships other than the relationship between the containers.



3. Laravel adopts the Classmap method to carry out its own active loading (autoload). Although PHP has a namespace this advanced feature, but this is only a logical relationship, require files still have to be. The corresponding relationship between this class and the file exists/vendor/composer/autoload_classmap.php, and each time the composer Dump-autoload is compiled and generated again.



Extended Reading

Summary of basic knowledge of PHP series of technical Articles to organize the collection
Summarize PHP Basics. To the beginning of the study of the person or master is worth the examination to consolidate.

1PHP How to check if a library or function is available

2 about PHP5 Session life cycle Introduction

Summary of UTF8 coding problems for 3PHP pages and MySQL databases

Configuration issues for 4WAMP5

5 Why PHP is popular with web developers

A solution to calculate the ratio of floating-point number in 6PHP and the accuracy of rounding

7php How to obtain client computer screen parameters

8 Getting started understanding of the concept of namespaces in PHP

9php5.4 The following version number JSON does not support the workaround for non-escaping content Chinese

The 12 most common questions involved in 10PHP interviews

11PHP function extension_loaded () Use method instance

12 about the accuracy loss of the PHP floating-point number

Using a regular form to extract Chinese implementation notes in 13PHP

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

15php implement traverse folder and delete specified contents in specified file

A brief analysis of namespace-related concepts in 16PHP

1750 tips for improving the efficiency of PHP operation "reprint"

18php Array function sequence sort () ascending the element values of the arrays

19PHP realization of a variety of Chinese-language encoding conversion class sharing

20PHP using MB_SUBSTR () to solve Chinese character string truncation garbled problem

Example of using Get_object_vars () method in 21php

22PHP Combine HTML5 use Formdata object to submit forms and upload images

23php parsing URLs using Parse_url and PARSE_STR

Introduction of several ways to implement encryption in 24PHP

25PHP Magic Variable and magic function specific explanation

26PHP return use method specific interpretation

27Java and PHP comparative analysis in web development

28PHP a summary of several ways to get post data

29 The 10 knowledge points that make PHP just start learning about people's confusion

30PHP Magic Method Usage Summary

31php:40+ Development Tools Recommended

Use header in 32php to set Content-type and common file type Content-type

Talk about namespace-related concepts in PHP

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.