Home php教程 php手册 PHP命名空间(namespace)的使用基础及示例

PHP命名空间(namespace)的使用基础及示例

Jun 06, 2016 pm 08:19 PM
namespace php Namespaces

本文介绍了PHP命名空间的一些术语,其解析规则,以及一些高级功能的应用,希望能够帮助读者在项目中真正使用命名空间。

PHP的命名空间(namespace)是PHP 5.3中加入最重要的一个新特性,这个概念在C#中已经很早就有了,php中的namespace其实和c#的概念是一样的。

一. PHP的命名空间主要解决三种冲突的问题:常量,函数,类

通俗理解:namespace就相当于是用来建一个目录,将namespace下面的代码放在该目录里面,与外面的区分开来。

/* |--------------------------------- |namespace示例 |@黑眼诗人 |--------------------------------- */ namespace myself; function var_dump() { echo 100; } var_dump();     //调用自定义函数(相对路径方式) \myself\var_dump();    //调用自定义函数(绝对路径方式) \var_dump(100); //调用全局的(系统的函数)

注意:namespace 之前不能有任何代码,除了declare();多个文件可以使用同一个命名空间,但同一命名空间下定义的内容是不能冲突的。namespace支持子命名空间,如:namespace \myself\good,相当于多级目录的概念。

二. 同一文件中存在多个命名空间的情况

1.

/** * 同一文件中若使用了多个命名空间,一般写法 */ namespace nihao\shijie; function demo() {   //....... } namespace hello\world; function test() { //........ } \nihao\shijie\demo(); \hello\world\test();

2.

/** * 同一文件中若使用了多个命名空间,建议大括号扩起来 */ namespace nihao\shijie{   function test_one()   { //......   }; } namespace hello\world{   function test_two()   { //........   } } \nihao\shijie\test_one(); \hello\world\test_two();

同一文件内使用多个命名空间,主要用于项目将多个PHP脚本合并在同一文件中,实际中不提倡使用!

三. 名称解析规则(几个概念)

  1.非限定名称:名称中不包含命名空间分割符,如:myself

  2.限定名称:名称中含有命名空间分割符,,如:nihao\shijie

  3.完全限定名称:名称中包含分割符,并以命名空间分割符开始,如:\nihao\shijie  (即绝对路径的概念)

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles