Skip to content

Files

Latest commit

Nov 23, 2019
f6a44ce · Nov 23, 2019

History

History
44 lines (32 loc) · 694 Bytes

全局空间.md

File metadata and controls

44 lines (32 loc) · 694 Bytes

全局空间

如果没有定义任何命名空间,所有的类与函数的定义都是在全局空间,与 PHP 引入命名空间概念前一样。在名称前加上前缀 \ 表示该名称是全局空间中的名称,即使该名称位于其它的命名空间中时也是如此。

<?php

namespace Foo;

require __DIR__ . '/example.php';

/**
 * Call the global function.
 *
 * @param  void
 * @return string
 */
function bar(): string
{
    return \bar();
}

var_dump(bar()); // string(3) "bar"

上例中的 example.php 文件内容:

<?php

/**
 * Return a string.
 *
 * @param  void
 * @return string
 */
function bar(): string
{
    return 'bar';
}