PHP namespace concept parsing, php namespace parsing _php Tutorial

Source: Internet
Author: User
Tags autoload

PHP namespace concept parsing, PHP namespace parsing


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.

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) and improve 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.

http://www.bkjia.com/PHPjc/944872.html www.bkjia.com true http://www.bkjia.com/PHPjc/944872.html techarticle what is the namespace in PHP namespace resolution 1.PHP for PHP namespace concept resolution? What is a namespace? In a broad sense, namespaces are a way of encapsulating things. In the 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.