search
HomeBackend DevelopmentPHP TutorialSection 14 Namespace [14]_PHP Tutorial
Section 14 Namespace [14]_PHP TutorialJul 21, 2016 pm 04:10 PM
functionvariablenamenameanddifficultyeasyyesunderstandofspacekindwant


Naming variables, functions and classes can be difficult. In addition to having to take into account the easy-to-understand name of the variable, you also have to worry about whether the name has been used somewhere else. In a small script, the second question is the basic question . When you consider reusing your code, subsequent project code must avoid using the names you have used. Generally speaking, reusable code is always contained in functions or classes, and many naming conflicts that may occur need to be dealt with. But naming conflicts can also occur between functions and classes. You can try to avoid this by prefixing all classes, or you can use the namespace statement.

The Namespace keyword gives a block of code Name. Outside this code block, scripts must reference this code block using operator:: followed by the name of the namespace. The same method is used to reference static class members. Code within the namespace does not need to declare the namespace, it It is the default. This method is better than adding a prefix. Your code will become more compact and readable.

You may wonder whether it is possible to create hierarchical (nested) naming space. The answer is no. But you can add a colon after the namespace name, and you can call variables, functions and classes that do not contain a colon in the name again. Colons are allowed in namespaces as long as they are not the first character and the last character character or followed by another colon. Colons in namespace names have no meaning to PHP, but if you use them to distinguish logical blocks, they can be a good way to illustrate the parent-child relationship in your code. -child) relationship.


/* Note: You can use this:
namespace animal:dog {}
namespace animal:pig {}

Use colons Specify parent-child relationships.
*/


You may not include anything other than function, class, or constant definitions within a namespace statement. This will prevent you from using them to improve older A function library that uses global variables. Namespaces are best suited for object orientation. Constants within a namespace use the same syntax as constants in classes.

Example 6.17 shows how to use namespaces.

Listing 6.17 Using a namespace
namespace core_php:utility
{
class textEngine
{
public function uppercase($text) file://uppercase
{
return(strtoupper( $text));
}
}

//make non-OO interface Create a non-OO interface
function uppercase($text)
{
$e = new textEngine;
return($e->uppercase($text));
}

}

//test class in namespace Test the class in the namespace
$e = new core_php:utility::textEngine;
print($e->uppercase("from object") . "
");

//test function in namespace Test the function in the namespace
print(core_php:utility::uppercase("from function") . "
");

//bring class into global namespace Import the class Global namespace
import class textEngine from core_php:utility;
$e2 = new textEngine;
?> The Import statement imports a certain part of the namespace into the global namespace.
Required To import members of a single namespace, you can specify the type as constant, function or class, and then write the name of the member;
// For example, import class XXX
If you want to import all members of a specific type, you You can use * to replace the name;
//For example, import constant * imports all constants
If you want to import all members of all types, just use *.
//For example, import *

After the member, use the from keyword plus the name of the namespace.
//For example, import class textEngine from core_php:utility;

In short, you have to write something like import * from myNamespace or import class textEngine from A statement such as core_php:utility, as in Example 6.17.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/314213.htmlTechArticleNaming variables, functions and classes is quite difficult. In addition to taking into account that the names of variables should be easy to understand, Worry about whether this name has already been used somewhere else. In a small script, section...
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
机器学习基础之数字上的距离:点在空间中的距离机器学习基础之数字上的距离:点在空间中的距离Apr 11, 2023 pm 11:40 PM

本文转载自微信公众号「活在信息时代」,作者活在信息时代。转载本文请联系活在信息时代公众号。在机器学习中,一个基础的概念就是如何判断两个样本之间的差异,从而能够评价两个样本之间的相似性和类别等信息。而判断这种相似性的度量就是两个样本在特征空间内的距离。根据数据特征的不同,度量方法有很多种。一般而言,对两个数据样本x,y,定义一个函数d(x,y),如果定义其为两个样本之间的距离,那么d(x,y)则需要满足以下几条基本性质:非负性:d(x,y)>=0同一性:d(x,y)=0 ⇔ x=y对

C++ 函数命名中参数顺序的考虑C++ 函数命名中参数顺序的考虑Apr 24, 2024 pm 04:21 PM

在C++函数命名中,考虑参数顺序至关重要,可提高可读性、减少错误并促进重构。常见的参数顺序约定包括:动作-对象、对象-动作、语义意义和遵循标准库。最佳顺序取决于函数目的、参数类型、潜在混淆和语言惯例。

升级win11需要的C盘空间介绍升级win11需要的C盘空间介绍Dec 23, 2023 am 08:57 AM

众所周知,如果系统安装完成后占用系统盘太大,可能会导致系统运行卡顿、延迟,甚至是文件丢失。因此大家在安装win11系统之前,需要了解升级win11需要多大C盘空间,下面就跟着小编一起来看看吧。升级win11需要多大C盘空间:答:升级win11需要20-30GB的C盘空间。1、根据微软的win11配置要求可以看到,win11安装需要64GB的硬盘空间。2、但其实一般来说是不需要这么大的空间的。3、根据已经安装win11的用户反馈,win11升级大概需要20-30GB的C盘空间。4、但是如果我门只有

c盘剩余空间多大合适c盘剩余空间多大合适Jun 27, 2023 pm 02:51 PM

c盘剩余空间50-80G比较合适。由于系统在日后使用当中会产生垃圾文件,缓存文件等因此建议至少预留50GB-80GB的空间给C盘;如果您不习惯在安装软件时选择路径,日常也不是经常清理电脑,那么至少需要100GB。

如何在 Ubuntu 上添加交换空间 22.04 LTS如何在 Ubuntu 上添加交换空间 22.04 LTSFeb 20, 2024 am 11:12 AM

交换空间在Linux系统中扮演着重要角色,特别是在系统内存不足时。它充当着一个备用的内存存储空间,可以帮助系统平稳运行,即使在负载高的情况下也能保持稳定性。本文为您提供了在Ubuntu22.04LTS上添加交换空间的详细指南,以确保您的系统性能得到优化并能应对各种工作负载。了解交换空间交换空间提供虚拟内存,用于补充系统的物理RAM。当系统的RAM不足时,内核会将数据交换到磁盘,以防止内存不足和系统崩溃。Linux系统常用交换空间来处理这种情况。同时运行多个内存密集型应用程序处理非常大的文件或数据

内部错误:无法创建临时目录 [已解决]内部错误:无法创建临时目录 [已解决]Apr 17, 2023 pm 03:04 PM

Windows系统允许用户使用可执行/设置文件在您的系统上安装各种类型的应用程序。最近,许多Windows用户开始抱怨他们收到一个名为INTERNALERROR:cannotcreatetemporarydirectory在他们的系统上尝试使用可执行文件安装任何应用程序的错误。问题不仅限于此,而且还阻止用户启动任何现有的应用程序,这些应用程序也安装在Windows系统上。下面列出了一些可能的原因。运行可执行文件进行安装时不授予管理员权限。为TMP变量提供了无效或不同的路径。损坏的系

如何释放 Xbox Series S 或 Xbox Series X 上的空间如何释放 Xbox Series S 或 Xbox Series X 上的空间Apr 19, 2023 pm 01:46 PM

Xbox游戏机有大量可供下载和玩的游戏。再加上Microsoft的Xbox游戏通行证订阅,乐趣无穷无尽的游戏收藏永远不会停止。但是,存在可用于游戏的空间问题,XboxSeriesS为356GB,XboxSeriesX为850GB。虽然这在以前的游戏版本中很好,游戏的最大大小可能在20到45GB之间,这不是最近的游戏。最近可用的游戏最终占用了磁盘上的大量空间,从而使我们下载其他游戏的空间更少。ForzaHorizo​​n5和Halo5Guardians等

PHP是如何存储变量的?zval结构体你了解吗?PHP是如何存储变量的?zval结构体你了解吗?May 26, 2022 am 09:47 AM

在 PHP 中定义一个变量是不需要声明类型的,一开始给变量 $a 赋予一个整型值,后面又可以轻而易举地将其改变为其他类型。那在 PHP 的源码中是如何来存储这个变量 $a 的呢?带着这个疑问我们一起去看一看 PHP 的源码。

See all articles

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 agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools