PHP 声明子命名空间
简介
可以在命名空间中创建命名空间。 就像文件系统中的目录可以在层级结构中包含子目录一样,子命名空间也可以按层级排列。反斜杠字符 \用于定义顶级命名空间和子级命名空间之间的关系,
在此示例中,顶级命名空间 myspace 包含两个子命名空间 space1 和 space2。为了访问子命名空间中的函数/类,首先通过use 关键字使其可用
示例
<?php namespace myspace\space1; function hello() { echo "Hello World from space1
"; } namespace myspace\space2; function hello(){ echo "Hello World from space2
"; } use myspace\space1; hello(); use myspace\space2; hello(); ?>
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
输出
上面的代码显示了以下输出
Hello World from space2 Hello World from space2
广告