Home php教程 php手册 第十四节--命名空间

第十四节--命名空间

Jun 13, 2016 pm 12:37 PM
name space

/*
+-------------------------------------------------------------------------------+
| = 本文为Haohappy读> 
| = 中Classes and Objects一章的笔记 
| = 翻译为主+个人心得 
| = 为避免可能发生的不必要的麻烦请勿转载,谢谢 
| = 欢迎批评指正,希望和所有PHP爱好者共同进步! 
| = PHP5研究中心: http://blog.csdn.net/haohappy2004
+-------------------------------------------------------------------------------+
*/
命名空间在PHP5正式版本中被取消,本节内容无效--Haohappy注
第十四节--命名空间
命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用namespace语句.
Namespace关键字给一块代码命名. 在这个代码块外部,脚本必须用操作符::加上命名空间的名称来引用这个代码块. 引用静态的类成员也是用相同的方法. 在命名空间内代码不需要声明命名空间, 它本身就是默认的. 这种方法比添加前缀的方法好. 你的代码可由此变得更加紧凑和可读.
你可能想知道是否可以建立分层的(嵌套的)命名空间. 答案是不可以. 但你可以在命名空间名称后加上冒号, 你可以再次调用在名称中不包含冒号的变量,函数和类. 命名空间允许存在冒号,只要不是第一个字符和最后一个字符或接着另一个冒号. 命名空间的名称中的冒号对于PHP来说没有任何意义, 但如果你用他们来区分逻辑上的区块, 他们可以很好地说明你的代码中的父子(parent-child)关系.
/* 注: 即可以使用这样: 
namespace animal:dog {}
namespace animal:pig {}
用冒号来说明parent-child关系.
*/
你可能在一个命名空间语句内没有包含函数,类或常量定义以外的任何东西. 这将阻止你使用他们来改进旧的使用全局变量的函数库. 命名空间最适合于面向对象. 命名空间内的常量与类中的常量使用相同的语法. 
例子6.17显示了如何使用命名空间.
Listing 6.17 Using a namespace

复制代码 代码如下:

   namespace core_php:utility  
   {  
       class textEngine  
       {  
           public function uppercase($text) //大写  
           {  
               return(strtoupper($text));  
           }  
       }  
       //make non-OO interface 建立一个非OO的接口  
       function uppercase($text)  
       {  
           $e = new textEngine;  
           return($e->uppercase($text));  
       }  
   }  
   //test class in namespace 测试命名空间中的类  
   $e = new core_php:utility::textEngine;  
   print($e->uppercase("from object") . "
");  
   //test function in namespace 测试命名空间中的函数  
   print(core_php:utility::uppercase("from function") . "
");  
   //bring class into global namespace 把类导入全局命名空间  
   import class textEngine from core_php:utility;  
   $e2 = new textEngine;  
?>  

Import语句把命名空间中的某个部份导入全局的命名空间. 
要导入单一的命名空间的成员,可以指定类型为constant,function或class,接着写上成员的名称;
//如import class XXX
如果你想导入某一特定类型的所有成员,你可以用*来代替名称;
//如 import constant * 导入所有常量
如果你想导入所有类型的所有成员,用*即可. 
//如 import *
在成员之后,用from关键字加上命名空间的名称.
//如 import class textEngine from core_php:utility;
总之你要写成像import * from myNamespace或 import class textEngine from core_php:utility这样的语句,就像例6.17中那样. 
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 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)

Numerical distance based on machine learning: the distance between points in space Numerical distance based on machine learning: the distance between points in space Apr 11, 2023 pm 11:40 PM

This article is reprinted from the WeChat public account "Living in the Information Age". The author lives in the information age. To reprint this article, please contact the Living in the Information Age public account. In machine learning, a basic concept is how to judge the difference between two samples, so that the similarity and category information between the two samples can be evaluated. The measure to judge this similarity is the distance between two samples in the feature space. There are many measurement methods based on different data characteristics. Generally speaking, for two data samples x, y, define a function d(x, y). If it is defined as the distance between the two samples, then d(x, y) needs to satisfy the following basic properties : Non-negativity: d(x, y)>=0 Identity: d(x, y)=0 ⇔ x=y pair

How to add swap space on Ubuntu 22.04 LTS How to add swap space on Ubuntu 22.04 LTS Feb 20, 2024 am 11:12 AM

Swap space plays an important role in Linux systems, especially when the system is low on memory. It acts as a backup memory storage space that helps the system run smoothly and maintain stability even under high load. This article provides you with a detailed guide to adding swap space on Ubuntu 22.04LTS to ensure that your system performance is optimized and can handle various workloads. Understanding Swap Space Swap space provides virtual memory that is used to supplement the system's physical RAM. When the system is low on RAM, the kernel swaps data to disk to prevent out-of-memory and system crashes. Linux systems commonly use swap space to handle this situation. Run multiple memory-intensive applications simultaneously to process very large files or data

Considerations for parameter order in C++ function naming Considerations for parameter order in C++ function naming Apr 24, 2024 pm 04:21 PM

In C++ function naming, it is crucial to consider parameter order to improve readability, reduce errors, and facilitate refactoring. Common parameter order conventions include: action-object, object-action, semantic meaning, and standard library compliance. The optimal order depends on the purpose of the function, parameter types, potential confusion, and language conventions.

What is the appropriate amount of remaining space on the c drive? What is the appropriate amount of remaining space on the c drive? Jun 27, 2023 pm 02:51 PM

The remaining space on the c drive is 50-80G which is more suitable. Since the system will generate junk files, cache files, etc. in the future, it is recommended to reserve at least 50GB-80GB of space for the C drive; if you are not used to choosing a path when installing software and do not clean your computer frequently, then at least 100GB is required. .

Introduction to the C drive space required for upgrading win11 Introduction to the C drive space required for upgrading win11 Dec 23, 2023 am 08:57 AM

As we all know, if the system disk occupied is too large after the system installation is completed, it may cause system lags, delays, and even file loss. Therefore, before you install the win11 system, you need to know how much C drive space is required to upgrade win11. Let’s take a look with the editor. How much C drive space is required to upgrade win11: Answer: Upgrading win11 requires 20-30GB of C drive space. 1. According to Microsoft’s win11 configuration requirements, you can see that win11 installation requires 64GB of hard drive space. 2. But in fact, generally speaking, there is no need for such a large space. 3. According to feedback from users who have already installed win11, the win11 upgrade requires about 20-30GB of C drive space. 4. But if our door only has

How to free up space on Xbox Series S or Xbox Series X How to free up space on Xbox Series S or Xbox Series X Apr 19, 2023 pm 01:46 PM

The Xbox console has a huge selection of games to download and play. Coupled with Microsoft's Xbox Game Pass subscription, the fun never stops with your game collection. However, there is the issue of space available for games, which is 356GB on Xbox Series S and 850GB on Xbox Series X. While this was fine in previous versions of the game, the maximum size of the game was probably between 20 and 45GB, which isn't the case with recent games. Recently available games end up taking up a lot of space on the disk, leaving us less space to download other games. ForzaHorizon5 and Halo5Guardians and more

iOS 17.2: How to record spatial video on iPhone 15 Pro iOS 17.2: How to record spatial video on iPhone 15 Pro Dec 15, 2023 pm 01:09 PM

If you have an iPhone 15 or iPhone 15 Pro Max, iOS 17.2 lets you record spatial video for viewing in the Photos app on Apple's upcoming Vision Pro headphones. Here's how you do it. Apple's VisionPro headphones are expected to be released around February 2024. Until then, one way you can prepare for this is to use your iPhone to record video in a special format called spatial video, which can be viewed on Apple's headphones. Spatial videos appear as normal videos when viewed on an iPhone, but they offer near three-dimensionality on VisionPro

A review of artificial intelligence technology in cyberspace security A review of artificial intelligence technology in cyberspace security Apr 11, 2023 pm 04:10 PM

1. Introduction Due to the explosive growth of current computer networks, the ensuing problem is the rapidly increasing number of network attacks. Various sectors of our society, from government departments to various critical infrastructures in society, are heavily dependent on computer networks and information technology. Apparently they are also vulnerable to cyberattacks. Typical network attacks disable the target computer, take services offline, or access the target computer's data. The number and impact of cyberattacks has increased significantly since the 1990s. Network security refers to a set of technologies used to protect network device activities and measures to protect them from all possible threats. In traditional network security technology, most of them are static access management, and the security control system will protect based on preset definitions.

See all articles