PHP Namespaces Overview and definitions

Source: Internet
Author: User

Namespaces Overview

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.

In PHP, namespaces are used to solve two types of problems encountered when writing a class library or application to create reusable code such as classes or functions:

User-written code conflicts with the name of a class/function/constant or third-party class/function/constant inside PHP.

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

The PHP namespace provides a way to group related classes, functions, and constants together. The following is an example of a PHP namespace syntax:

Example #1 Namespace Syntax example

<?php    namespace My\name;     Class MyClass {}    function MyFunction () {}    const MYCONST = 1;    $a = new MyClass;    $c = new \my\name\myclass;     $a = strlen (' Hi ');     $d = Namespace\myconst;     $d = __namespace__. ' \myconst ';    ECHO constant ($d);?>

PHP supports namespaces at the beginning of the 5.3.0 version.

Define namespaces

Although any valid PHP code can be included in a namespace, only three types of code are affected by the namespace, which are: classes, functions, and constants.

The namespace is declared by the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code.

Example #1 declaring a single namespace

<?php    namespace MyProject;    Const CONNECT_OK = 1;    Class Connection {/* ... */}    function Connect () {/* ... */}?>

The only valid code before declaring the namespace is the Declare statement that defines how the source file is encoded. In addition, all non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace:

Example #2 declaring a single namespace


In addition, unlike other language features in PHP, the same namespace can be defined in multiple files, allowing the content of the same namespace to be split into separate files.

Defining child namespaces

As with directories and files, the PHP namespace allows you to specify the name of a hierarchical namespace. Therefore, the name of the namespace can be defined in a hierarchical way:

Example #1 declaring a hierarchical, single namespace

<?php    namespace Myproject\sub\level;    Const CONNECT_OK = 1;    Class Connection {/* ... */}    function Connect () {/* ... */}?>

The above example creates constants Myproject\sub\level\connect_ok, class Myproject\sub\level\connection, and function myproject\sub\level\connection.

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