A detailed introduction to the namespace in php and the php namespace

Source: Internet
Author: User
Tags php website

A detailed introduction to the namespace in php and the php namespace

Php namespaceThe most explicit function of PHP is to solve the problem of duplicate names. In PHP, two functions or classes cannot have the same name. Otherwise, a fatal error occurs. The previous section describes what a php namespace is. The official php website has clearly defined and visualized explanations. Here we copy a text from the official php Website: in a broad sense, namespace is a way to encapsulate things. This abstract concept can be seen in many places. For example, a directory in the operating system is used to group related files. For files in the directory, it plays the role of namespace. For example, the file foo.txt can exist in the/home/greg and/home/other directories at the same time, but there cannot be two foo.txt files in the same directory. In addition, when accessing the foo.txt file outside the/home/greg directory, we must put the directory name and directory separator before the file name to get/home/greg/foo.txt. This principle is applied to the field of programming as a namespace concept.

Php namespace IntroductionNamespace keywordThis is to solve the various "troubles" that have occurred in php object-oriented programming. The specific troubles are as follows:

  1. The user-written code conflicts with the names of PHP internal classes/functions/constants or third-party classes/functions/constants.

  2. To ease the trouble 1, we usually use long class names when writing various classes or add name prefixes (or suffixes) for classes that implement different functions ).

  3. When the magic function _ autoload is not used, and each class excludes a PHP file, in order to call different classes, multiple include (or require or require_once) statements are written at the beginning of the other PHP files using these classes.

First, create a namespace. multiple namespaces can be created in the same script file.

The Code is as follows:

<? Php
// Create a namespace named 'Article'
Namespace Article;
// This Comment is an element of the Article space.
Class Comment {}
// Create a namespace named 'messageboard'
Namespace MessageBoard;
// This Comment is an element of the MessageBoard space.
Class Comment {}
?>

You cannot directly call other elements between different spaces. You need to use the namespace syntax.

The sample code is as follows:

<? Php
Namespace Article;
Class Comment {}
Namespace MessageBoard;
Class Comment {}
// Call the Comment class of the current space (MessageBoard)
$ Comment = new Comment ();
// Call the Comment class of the Article Space
$ Article_comment = new \ Article \ Comment ();
?>

We can see that when the Comment class in the article space is called in the MessageBoard space, a syntax like file path is used:\ Space name \ element name

In addition to classes, functions and constants are used in the same way. Now I create new elements for the two spaces and output their values in the MessageBoard space.

The Code is as follows:

<? Php
Namespace Article;
Const PATH = '/article ';
Function getCommentTotal (){
Return 100;
}
Class Comment {}
Namespace MessageBoard;
Const PATH = '/message_board ';
Function getCommentTotal (){
Return 300;
}
Class Comment {}
// Call constants, functions, and classes of the current space
Echo PATH; // message_board
Echo getCommentTotal (); // 300.
$ Comment = new Comment ();
// Call constants, functions, and classes in the Article Space
Echo \ Article \ PATH; // article
Echo \ Article \ getCommentTotal (); // 100
$ Article_comment = new \ Article \ Comment ();
?>

The following result is displayed:/Message_board300/article100

[Recommended tutorials]

1. php.cn Dugu jiujian (4)-php video tutorial

2. video tutorial: namespace: although we have the same name but belong to different time and space

3. Complete php programming tutorials from getting started to mastering

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.