2011-01-25 98 views
97

\在PHP中做什么?PHP(5.3+)有什么(反斜杠)?

例如,https://github.com/foxbunny/CSRF4PHP/blob/60d9172b7f0cd93346cac9065fb17182854ebf1c/CsrfToken.php#L80-L87具有\FALSE\session_id,和\Exception

public function __construct($timeout=300, $acceptGet=\FALSE){ 
    $this->timeout = $timeout; 
    if (\session_id()) { 
     $this->acceptGet = (bool) $acceptGet; 
    } else { 
     throw new \Exception('Could not find session id', 1); 
    } 
} 
+32

它使代码不易读。但对于[参考:这个符号在PHP中意味着什么](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – mario 2011-01-25 04:37:00

+0

@mario非常漂亮的stackoverflow主题。感谢您的链接:) – Alfred 2011-01-25 04:39:28

回答

132

\(反斜杠)是在PHP 5.3名称空间隔离。

A \函数开始之前代表Global Namespace

将它放在那里将确保所调用的函数来自全局名称空间,即使在当前名称空间中有一个相同名称的函数。

8

澄清潜在的混乱:

反斜杠并不意味着继承

在下面,Animal,Dog,Shepherd不必是类,但只需要namespaces。意思是用于将名称组合在一起到avoid naming collisions

$myDog = new \Animal\Dog\Shepherd\GermanShepherd(); 

领先\意味着Animal在全球范围内宣布。