Home Backend Development PHP Tutorial Declaration and use of namespaces in php

Declaration and use of namespaces in php

Jun 28, 2018 pm 03:52 PM

* Declaration and use of namespace

* 1. Use keywords: namespace

* 2.php5.3

* 3. There cannot be anything else except namespace before Any code other than declare and comments, including HTML, will not work

* 4. The namespace starts from the global position by default, and the global space is represented by \, similar to the root directory

* 5. If the current If the script declares a namespace, access to all members must use the namespace, including system functions

//1. Declare a namespace

1

2

3

4

namespace one;

class Demo{public $name='Peter Zhu';}

const SITE='PHP中文网';

function add($a,$b){return $a+$b;}

Copy after login


//If I want to declare a class with the same name, constants and functions can declare another namespace

//2. Multiple namespaces are allowed in a script

//Declaration of namespaces :two, use \tow\ for reference

1

namespace two;

Copy after login

//Now declare the class Demo, constant SITE, and function add, there will be no conflict

1

2

3

class Demo{public $name='朱老师';}

const  SITE = 'www.php.cn';

function add($a,$b){return $a+$b;}

Copy after login

//I believe you can see this, this is very For example, files with the same name are allowed to be created in different directories. The principle is the same.

1

echo (new Demo)->name, &#39;<br>&#39;; //默认输出的是当前空间的内容

Copy after login

//Check what the current namespace is? Use the system preset constant: __NAMESPACE__

1

echo &#39;当前命名空间是: &#39;,__NAMESPACE__,&#39;<br>&#39;;

Copy after login

/ /You can also bring the current namespace to access members in the current space

//Generate a class name with the current namespace

1

2

$className = __NAMESPACE__.&#39;\Demo&#39;;

echo (new $className)->name,&#39;<br>&#39;;

Copy after login

//You can bring the complete namespace name , starting from the global space\, which is equivalent to starting from the root directory

//Professional terminology: fully qualified name

1

echo (new \two\Demo)->name, &#39;<hr>&#39;;

Copy after login

//3. How to achieve cross-space access?

//If we want to access the attributes in the Demo class in space one, how do we do it?

//Cross-space access, like cross-directory access to files, must bring its complete space path

//For example, we want to access members of class Demo in one space

1

echo &#39;one空间的类成员:&#39;,(new \one\Demo)->name,&#39;<br>&#39;;

Copy after login

//4. In a script that declares a namespace, how to access system predefined methods?

//For example, a single-character array is declared

1

2

3

$welcome = &#39;php中文网欢迎您&#39;;

\print_r($welcome); echo &#39;<br>&#39;;

print($welcome); echo &#39;<hr>&#39;;

Copy after login

//We just said that to access global members in space, you must add \. Why is it okay if it is not added here?

// Because if the user does not add it, it will first search for the var_dump() function in this space.

//If it is not found, it will then search in the global space, so no error will be reported.

// But if we also create a var_dump() function in the current two space, only the user-defined var_dump() in the current space will be executed, and the system function with the same name will not be called

1

2

3

4

5

function print_r($arg)

{

    echo &#39;我是当前空间声明的函数调用:&#39;.$arg;

}

print_r($welcome); echo &#39;<br>&#39;;

Copy after login

//Equivalent calling syntax:

1

\two\print_r($welcome);

Copy after login
Copy after login

//Note: This print_r() has nothing to do with the system’s built-in print_r(), it just has the same name

// Just like, there is a Yangtze River Road in Shanghai, and there is a Yangtze River Road in Hefei. Apart from the same name, these two roads have no relationship whatsoever

// Yangtze River Road, Baoshan District, Shanghai

// Changjiang Road, Luyang District, Hefei City

// I believe no one will think that this is the same road?

//In fact, the complete calling statements of these two functions should be like this:

//1. Call the system:

1

\print_r($welcome);

Copy after login

//2. Call the current space:

1

\two\print_r($welcome);

Copy after login
Copy after login

//Although the subsequent names are the same, the ownership is different

The above is the detailed content of Declaration and use of namespaces in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solve PHP error: The specified namespace class was not found Solve PHP error: The specified namespace class was not found Aug 18, 2023 pm 11:28 PM

Solve PHP error: The specified namespace class was not found. When developing using PHP, we often encounter various error messages. One of the common errors is "The specified namespace class was not found". This error is usually caused by the imported class file not being properly namespace referenced. This article explains how to solve this problem and provides some code examples. First, let’s take a look at an example of a common error message: Fatalerror:UncaughtError:C

How to use namespace in F3 framework? How to use namespace in F3 framework? Jun 03, 2023 am 08:02 AM

The F3 framework is a simple, easy-to-use, flexible and scalable PHPWeb framework. Its namespace (Namespace) mechanism provides us with a more standardized, more readable, and clearer code structure. In this article, we will explore how to use namespaces in the F3 framework. 1. What is a namespace? Namespaces are often used to solve the problem of naming conflicts in PHP. It can encapsulate one or more classes, functions or constants in a namespace, which is equivalent to adding a prefix to them. example

Design ideas and implementation methods of Redis namespace and expiration mechanism Design ideas and implementation methods of Redis namespace and expiration mechanism May 11, 2023 am 10:40 AM

Redis is an open source, high-performance key-value storage database. When using Redis for data storage, we need to consider the design of the key namespace and expiration mechanism to maintain Redis performance and data integrity. This article will introduce the design ideas and implementation methods of Redis' namespace and expiration mechanism. 1. Redis namespace design ideas In Redis, keys can be set arbitrarily. In order to facilitate the management and distinction of different data types, Redis introduces the concept of namespace. Life

C++ syntax error: undefined namespace used, how to deal with it? C++ syntax error: undefined namespace used, how to deal with it? Aug 21, 2023 pm 09:49 PM

C++ is a widely used high-level programming language. It has high flexibility and scalability, but it also requires developers to strictly master its grammatical rules to avoid errors. One of the common errors is "use of undefined namespace". This article explains what this error means, why it occurs, and how to fix it. 1. What is the use of undefined namespace? In C++, namespaces are a way of organizing reusable code in order to keep it modular and readable. You can use namespaces to make functions with the same name

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Sep 11, 2023 pm 12:22 PM

Example of new features in PHP8: How to use namespaces and codes to better organize the code structure? Introduction: PHP8 is an important version of the PHP programming language, which introduces many exciting new features and improvements. One of the most important new features is namespaces. Namespaces are a way to organize your code into a better structure that avoids conflicts between classes, functions, and constants with the same name. In this article, we’ll look at how to leverage namespaces and codes to better structure your PHP8 code

Namespace configuration and application examples in PHP Namespace configuration and application examples in PHP Jun 25, 2023 am 08:32 AM

PHP is a highly flexible programming language with a wide range of applications. In PHP development, in order to avoid naming conflicts and improve the readability and maintainability of code, PHP introduces the concept of namespace. Namespaces help developers use the same class or function name in the same project without conflict. This article will introduce how to configure namespaces in PHP and common application examples. 1. How to configure the PHP namespace. Declare the namespace in PHP by using namespa at the top of the file.

PHP 5.3 new feature: How to use namespaces to resolve class name conflicts PHP 5.3 new feature: How to use namespaces to resolve class name conflicts Jul 30, 2023 pm 12:25 PM

New features of PHP5.3: How to use namespaces to solve class name conflicts Introduction: During the development of PHP, as projects become larger and more complex, class name conflicts also arise. In order to solve this problem, PHP5.3 version introduced the concept of namespace. Namespaces provide a way to organize related classes, functions, and constants together to avoid naming conflicts. This article will introduce in detail the concept of PHP namespaces and how to use namespaces to solve class name conflicts, with code examples.

Methods to solve PHP namespace errors and generate corresponding error prompts Methods to solve PHP namespace errors and generate corresponding error prompts Aug 07, 2023 pm 05:16 PM

How to resolve PHP namespace errors and generate corresponding error messages. PHP is a widely used server-side scripting language that is used to develop web applications. In PHP, namespace (Namespace) is a mechanism for managing and organizing code, which can avoid naming conflicts and improve code readability and maintainability. However, the complexity of namespace definition and use sometimes leads to errors. This article will introduce some methods to solve PHP namespace errors and generate corresponding error prompts. 1. Name space

See all articles