PHP 全域性空間


簡介

在沒有任何名稱空間定義的情況下,類、函式等的所有定義都放置在全域性名稱空間中。如果任何名稱字首為 \,則表示即使在名稱空間的上下文中也需要從全域性空間中獲取該名稱。

使用全域性空間規範

示例

<?
namespace test;
/* This function istest\fopen */
function fopen() {
   /* ... */
   $f = \fopen(...); // call global fopen
   return $f;
}
?>

包含的檔案將預設在全域性名稱空間中。

示例

#test1.php
<?php
echo __NAMESPACE__ . "
"; ?>

以下將列印空字串

當該檔案包含在其他名稱空間中時

示例

#test2.php
<?php
namespace testspace {
   include 'test1.php';
   echo __NAMESPACE__ . "
"; } ?>

輸出

以下將列印如下輸出

testspace

更新於: 2020 年 9 月 18 日

205 次瀏覽

開啟你的 事業

參加課程獲得認證

開始
廣告
© . All rights reserved.