PHP namespace and auto-load Analysis namespace thinkphp namespace php 5.3 php namespace us

Source: Internet
Author: User
Tags php class
Name space

What is a namespace?

The namespace (English: Namespace) represents the visible range of identifiers (identifier). An identifier can be defined in more than one namespace, and its meaning in different namespaces is mutually irrelevant. In this way, any identifiers can be defined in a new namespace, and they do not conflict with any existing identifiers because existing definitions are in other namespaces.

Namespaces are simply meant to address naming conflicts that occur when different libraries are used in the same environment. For example, I have a function called a, but the system already has a function, then there will be a conflict.

Namespaces in PHP

PHP introduced namespaces starting with version 5.3, and many of the existing PHP class libraries and frameworks began to support it. So how does the PHP namespace work?

Define namespaces

Here is an example of defining a namespace

 
  

In the example above, a typical namespace definition method, only, const is function class constrained by a namespace.

Using namespaces

The namespaces are used in the following ways:

 
  

The first thing to note is that the namespace is only a declaration, that is, when using a namespace, you still have to include the file that the namespace declares. In use, you can __NAMESPACE__ view the current namespace by using the.

See the official PHP documentation for more information

Auto Load

Each file is to declare the name of the control include and manual is very intelligent things, so in their own PHP system or framework can use the automatic loading technology, let the system to find their own

The simplest way is to take advantage of function __autoload functions, but this function can only be defined under a non-named control, that is, in the global context:

function __autoload ($class) {  $dir = './';  Set_include_path (Get_include_path (). Path_separator. $ids _dir);  $class = str_replace (' \ \ ', '/', $class). '. php ';   Require_once ($class); }

If you are in a class that already has a namespace, you can use a function spl_autoload_register to register a method in a class instead of__autoload

The above introduces the PHP namespace and automatic loading analysis, including the namespace, PHP content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.