try {} catch(){}中的(Exception $ e)和(\ Exception $ e)之间的区别 [英] Difference between (Exception $e) and (\Exception $e) in try{}catch(){}
问题描述
try {} catch(){}
中的(Exception $ e)
和(\ Exception $ e)
有什么区别?
在 Exception
之前加反斜杠\"有什么影响?
What is the difference between (Exception $e)
and (\Exception $e)
in try{}catch(){}
What is the impact of 'back-slash \' before Exception
?
推荐答案
在类名前面使用 \
,这意味着您从全局空间调用 class
.如果不使用 \
,它将在与代码相同的名称空间中调用该类.但是,如果您在代码中不使用 namespace
,它将从全局空间调用类.
Using \
in front of class name, it mean you call the class
from global space. If you don't use \
, it will call the class in same namespace with your code. But if you don't use namespace
in your code, it will call class from global space.
示例:
<?php
namespace Module\Example;
class Test
{
try{
} catch(Exception $e) { // will look up Module\Example\Exception
}
try{
} catch(\Exception $e) { // will look up Exception from global space
}
}
您可以查看此文档以获取更多详细信息. http://php.net/manual/en/language.namespaces.global.php
You can check this documentation for more detail. http://php.net/manual/en/language.namespaces.global.php
这篇关于try {} catch(){}中的(Exception $ e)和(\ Exception $ e)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!