Home >Backend Development >PHP Tutorial >Parse namespace in PHP

Parse namespace in PHP

autoload
autoloadOriginal
2021-04-16 11:41:523466browse

Parse namespace in PHP

Namespacenamespace refers to the artificial separation of memory so that structures with the same name in different memory areas can coexist, thereby solving the problems that may arise in large projects. Name structure problem. This article will take you to take a look at namespace.

In PHP, functions, classes, constants are not allowed to have the same name. In order to solve the problem of the same name among these three, namespace appeared, so namespace only affects class, function,Constant(const).

1. Basic syntax:

<?php
    namespace my_self;			//定义一个叫做my_self的空间
     内容                        
    namespace space;                      //定义一个叫做space的空间
     内容                   //可以同时命名多个 
?>

2. Namespace naming rules

  • Consists of letters, underscores and numbers

  • Can start with letters and underscores

  • ##Less When a multi-word space name appears, the underscore method is generally used

  • <?php
        namespace 3df;//报错
        namespace _k3;//正确
        namespace ak47;//正确
    ?>

3. The first declaration of the namespace must be before all codes

<?php
namespace space1;//namespace关键字+空间名
代码内容;
 ?>

But there are exceptions to everything. The only legal code before declaring a namespace is the

declare statement used to define the encoding method of the source file. All non-PHP code, including whitespace, cannot appear before the declaration of the namespace.

Recommendation: 2021 PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of Parse namespace in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn