CakeFest 2024: The Official CakePHP Conference

ReflectionClass::getNamespaceName

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

ReflectionClass::getNamespaceName获取命名空间的名称

说明

public ReflectionClass::getNamespaceName(): string

获取命名空间(namespace)的名称。

参数

此函数没有参数。

返回值

命名空间的名称。

示例

示例 #1 ReflectionClass::getNamespaceName() 示例

<?php
namespace A\B;

class
Foo { }

$class = new \ReflectionClass('stdClass');

var_dump($class->inNamespace());
var_dump($class->getName());
var_dump($class->getNamespaceName());
var_dump($class->getShortName());

$class = new \ReflectionClass('A\\B\\Foo');

var_dump($class->inNamespace());
var_dump($class->getName());
var_dump($class->getNamespaceName());
var_dump($class->getShortName());
?>

以上示例会输出:

bool(false)
string(8) "stdClass"
string(0) ""
string(8) "stdClass"

bool(true)
string(7) "A\B\Foo"
string(3) "A\B"
string(3) "Foo"

add a note

User Contributed Notes 1 note

up
7
francois
12 years ago
If the object does not belong to a namespace, an empty string is returned
To Top