Home Backend Development PHP Tutorial 第十四节--命名空间 -- Classes and Objects in PHP5 [14]_PHP

第十四节--命名空间 -- Classes and Objects in PHP5 [14]_PHP

Jun 01, 2016 pm 12:33 PM
code use Can name name space

第十四节--命名空间

命名变量,函数和类是挺困难的,除了要考虑到变量的名称要易于理解,还要担心是否这个名称在其它某个地方已经使用过. 在一小段脚本中,第二个问题是基本问题. 当你考虑重用你的代码, 在这之后的项目代码必须避免使用你用过的命名. 通常来说,可重用的代码总是包含在函数或类中, 需要处理许多可能发生的命名冲突. 但函数和类之间也可能发生命名冲突. 你可以尝试避免出现这种情况,通过在所有类前添加前缀的方法,或者你可以使用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

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)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1266
29
C# Tutorial
1239
24
3 tips to easily customize your Mac computer name, host name and Bonjour name 3 tips to easily customize your Mac computer name, host name and Bonjour name Mar 06, 2024 pm 12:20 PM

Many Mac users tend to keep the default name of their device and may never consider changing it. Many people choose to stick with the name from the initial setup, such as "Johnny's MacBook Air" or simply "iMac." Learning how to change the name of your Mac is a very useful skill, especially when you have multiple devices, as it can help you quickly distinguish and manage them. Next, we will teach you step by step how to change the computer name, host name and Bonjour name (local host name) in macOS system. Why should you change your Mac name? Changing the name of your Mac can not only show your personality, but also help improve the user experience: Personalize your Mac: The default name may not be to your taste, change it to a name you like.

What software is crystaldiskmark? -How to use crystaldiskmark? What software is crystaldiskmark? -How to use crystaldiskmark? Mar 18, 2024 pm 02:58 PM

CrystalDiskMark is a small HDD benchmark tool for hard drives that quickly measures sequential and random read/write speeds. Next, let the editor introduce CrystalDiskMark to you and how to use crystaldiskmark~ 1. Introduction to CrystalDiskMark CrystalDiskMark is a widely used disk performance testing tool used to evaluate the read and write speed and performance of mechanical hard drives and solid-state drives (SSD). Random I/O performance. It is a free Windows application and provides a user-friendly interface and various test modes to evaluate different aspects of hard drive performance and is widely used in hardware reviews

How to download foobar2000? -How to use foobar2000 How to download foobar2000? -How to use foobar2000 Mar 18, 2024 am 10:58 AM

foobar2000 is a software that can listen to music resources at any time. It brings you all kinds of music with lossless sound quality. The enhanced version of the music player allows you to get a more comprehensive and comfortable music experience. Its design concept is to play the advanced audio on the computer The device is transplanted to mobile phones to provide a more convenient and efficient music playback experience. The interface design is simple, clear and easy to use. It adopts a minimalist design style without too many decorations and cumbersome operations to get started quickly. It also supports a variety of skins and Theme, personalize settings according to your own preferences, and create an exclusive music player that supports the playback of multiple audio formats. It also supports the audio gain function to adjust the volume according to your own hearing conditions to avoid hearing damage caused by excessive volume. Next, let me help you

BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? BTCC tutorial: How to bind and use MetaMask wallet on BTCC exchange? Apr 26, 2024 am 09:40 AM

MetaMask (also called Little Fox Wallet in Chinese) is a free and well-received encryption wallet software. Currently, BTCC supports binding to the MetaMask wallet. After binding, you can use the MetaMask wallet to quickly log in, store value, buy coins, etc., and you can also get 20 USDT trial bonus for the first time binding. In the BTCCMetaMask wallet tutorial, we will introduce in detail how to register and use MetaMask, and how to bind and use the Little Fox wallet in BTCC. What is MetaMask wallet? With over 30 million users, MetaMask Little Fox Wallet is one of the most popular cryptocurrency wallets today. It is free to use and can be installed on the network as an extension

How to use NetEase Mailbox Master How to use NetEase Mailbox Master Mar 27, 2024 pm 05:32 PM

NetEase Mailbox, as an email address widely used by Chinese netizens, has always won the trust of users with its stable and efficient services. NetEase Mailbox Master is an email software specially created for mobile phone users. It greatly simplifies the process of sending and receiving emails and makes our email processing more convenient. So how to use NetEase Mailbox Master, and what specific functions it has. Below, the editor of this site will give you a detailed introduction, hoping to help you! First, you can search and download the NetEase Mailbox Master app in the mobile app store. Search for "NetEase Mailbox Master" in App Store or Baidu Mobile Assistant, and then follow the prompts to install it. After the download and installation is completed, we open the NetEase email account and log in. The login interface is as shown below

How to use Baidu Netdisk app How to use Baidu Netdisk app Mar 27, 2024 pm 06:46 PM

Cloud storage has become an indispensable part of our daily life and work nowadays. As one of the leading cloud storage services in China, Baidu Netdisk has won the favor of a large number of users with its powerful storage functions, efficient transmission speed and convenient operation experience. And whether you want to back up important files, share information, watch videos online, or listen to music, Baidu Cloud Disk can meet your needs. However, many users may not understand the specific use method of Baidu Netdisk app, so this tutorial will introduce in detail how to use Baidu Netdisk app. Users who are still confused can follow this article to learn more. ! How to use Baidu Cloud Network Disk: 1. Installation First, when downloading and installing Baidu Cloud software, please select the custom installation option.

GE universal remote codes program on any device GE universal remote codes program on any device Mar 02, 2024 pm 01:58 PM

If you need to program any device remotely, this article will help you. We will share the top GE universal remote codes for programming any device. What is a GE remote control? GEUniversalRemote is a remote control that can be used to control multiple devices such as smart TVs, LG, Vizio, Sony, Blu-ray, DVD, DVR, Roku, AppleTV, streaming media players and more. GEUniversal remote controls come in various models with different features and functions. GEUniversalRemote can control up to four devices. Top Universal Remote Codes to Program on Any Device GE remotes come with a set of codes that allow them to work with different devices. you may

How to use Thunder to download magnet links How to use Thunder to download magnet links Feb 25, 2024 pm 12:51 PM

With the rapid development of network technology, our lives have also been greatly facilitated, one of which is the ability to download and share various resources through the network. In the process of downloading resources, magnet links have become a very common and convenient download method. So, how to use Thunder magnet links? Below, I will give you a detailed introduction. Xunlei is a very popular download tool that supports a variety of download methods, including magnet links. A magnet link can be understood as a download address through which we can obtain relevant information about resources.

See all articles