Talk about namespace-related concepts in PHP

Source: Internet
Author: User
Tags autoload

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. This abstract concept can be seen in many places. For example, in an operating system, a directory is used to group related files, and for files in a directory, it plays the role of a namespace. For example, file Foo.txt can exist in both directory/home/greg and/home/other, but there cannot be two foo.txt files in the same directory. In addition, when accessing the Foo.txt file outside of directory/home/greg, we must place the directory name and directory 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 place classes, functions, and variables that can be accessed unconditionally within the same namespace. Outside of the namespace, you must reference or import additional namespaces in order to invoke these items that they contain.

Namespaces are the same as the concept of a file directory in the shell. In the current directory you can access all files directly with the file name, if you need to access files in other directories, you need to enter a relative path or an 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 (usually 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, in the Pagecontroller class in Laravel, you can write code like Page::all () directly to call 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 can only be used in other classes that call the same current namespace without "referencing" or "importing," which belong to one. Any child namespace is another namespace, and the other container has no special relationships other than the relationships between the containers.

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



Extended Reading

Summary of basic knowledge of PHP series of technical Articles to organize the collection
Summing up the basic knowledge of PHP, beginners or experts are worthy of reference 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

Comparison of floating point calculation in 6PHP and the solution of inaccurate rounding

7php How to get client computer screen parameters

8 Getting started understanding of the concept of namespaces in PHP

9php5.4 The following versions of JSON do not support the solution of non-escaping content Chinese

The 12 most frequently involved questions in 10PHP related interviews

11PHP function extension_loaded () Usage Example

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

Use regular expressions in 13PHP to extract Chinese implementation notes

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

The 15php implementation iterates through the directory and deletes the specified content in the specified file

A brief analysis of namespace-related concepts in 16PHP

1750 tips to improve PHP execution efficiency "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 Get_object_vars () method usage 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 variables and Magic functions

Detailed interpretation of return usage in 26PHP

27Java and PHP comparative analysis in web development

28PHP a summary of several ways to get post data

29 confusing 10 points of knowledge for PHP beginners

30PHP Magic Method Usage Summary

31php:40+ Development Tools Recommended

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

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

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.