Section 1: Overview of namespaces
Version requirements
PHP introduces namespaces starting from version 5.3.0
What is a namespace
Quoting the example in the PHP official manual:
The file foo.txt can be in the directory / at the same time exists in home/greg and /home/other, but two foo.txt files cannot exist in the same directory. Additionally, when accessing the foo.txt file outside the directory /home/greg, we must put the directory name and directory separator before the file name to get /home/greg/foo.txt. The application of this principle to the field of programming is the concept of namespace.
The role of namespaces
In PHP, namespaces are used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:
User-written code and PHP Name conflicts between internal classes/functions/constants or third-party classes/functions/constants.
Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem), improving the readability of the source code.
Example of namespace syntax
<?phpnamespace my\name; // 参考 "定义命名空间" 小节 class MyClass {} function myfunction() {} const MYCONST = 1; $a = new MyClass; $c = new \my\name\MyClass; // 参考 "全局空间" 小节 $a = strlen('hi'); // 参考 "使用命名空间:后备全局函数/常量" 小节 $d = namespace\MYCONST; // 参考 "namespace操作符和__NAMESPACE__常量” 小节 $d = __NAMESPACE__ . '\MYCONST'; echo constant($d); // 参考 "命名空间和动态语言特征" 小节?>
Section 2 Definition of namespace
Definition of namespace
Although any legal PHP code can be included in a namespace, only the following types of code are affected by namespaces, they They are: classes (including abstract classes and traits), interfaces, functions and constants.
The namespace is declared through the keyword namespace. If a file contains a namespace, it must declare the namespace before all other code except one: the declare keyword.
The only legal code before declaring a namespace is the declare statement that defines how the source file is encoded. In addition, all non-PHP code, including whitespace, cannot appear before a namespace declaration.
In addition, unlike other language features of PHP, the same namespace can be defined in multiple files, which allows the contents of the same namespace to be divided and stored in different files.
Example of correct namespace:
<?phpnamespace MyProject; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } ?>
Example of incorrect namespace:
<html> <?php namespace MyProject; // 致命错误 - 命名空间必须是程序脚本的第一条语句 ?>
Defining sub-namespaces
Much like the relationship between directories and files, PHP namespaces also allow you to specify hierarchical namespace names. Therefore, namespace names can be defined in a hierarchical manner.
The following example creates the constant MyProjectSubLevelCONNECT_OK, the class MyProjectSubLevelConnection and the function MyProjectSubLevelconnect.
<?php namespace MyProject\Sub\Level; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } ?>
Define multiple namespaces in the same file
You can also define multiple namespaces in the same file. There are two syntaxes for defining multiple namespaces in the same file.
The recommended syntax is to use braces to define the namespace.
<?php namespace MyProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } } namespace AnotherProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } }?>
In actual programming practice, it is highly discouraged to define multiple namespaces in the same file. This method is mainly used to combine multiple PHP scripts in the same file.
Except for the opening declare statement, there must be no PHP code outside the namespace brackets.
The above is the content of Note 007 PHP namespace - the first part. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
