php 为什么new一个对象后面要加一个反斜杠 能帮忙解释一下代码吗

static function checkArg($argName, $value)
{
if ($value === null) throw new \InvalidArgumentException("'$argName' must not be null");
if (!is_string($value)) throw new \InvalidArgumentException("'$argName' must be a string");
$error = self::findError($value);
if ($error !== null) throw new \InvalidArgumentException("'$argName'': bad path: $error: ".var_export($value, true));
}

通俗点解释就是 指定对象具体的类型。
在没有引入将类文件引入当前文件时,php无法知晓你需要使用的是什么类(相同命名空间的类文件除外),所以 为了防止 存在多个同名类文件,php需要你指定具体的类文件,带反斜杠其实就是(命名空间)路径的表示。
温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2017-11-29

因为使用了php的命名空间,演示代码如下:

// 创建命名空间Article
namespace Article;
class Comment { }

// 创建命名空间MessageBoard
namespace MessageBoard;
class Comment { }

//调用当前空间(MessageBoard)的Comment类
$comment = new Comment();

//调用Article空间的Comment类
$article_comment = new \Article\Comment();

本回答被提问者采纳
第2个回答  2020-10-23

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网