如何在PHP 5.3+中的命名空间类中使用全局命名空间类型提示? [英] How do I use global namespace type hinting inside of a namespaced class in PHP 5.3+?

查看:126
本文介绍了如何在PHP 5.3+中的命名空间类中使用全局命名空间类型提示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace MyClass\Util;

class Sample {

  public function each(Object $f) {

  }
}


从调用文件(未命名空间)


From Calling File (not namespaced)

$sample = new Sample();
$sample->each(new stdClass());

产生:

可捕获的致命错误:传递给MyClass \ Util \ Sample.php的参数1必须是MyClass \ Util \ Object的实例,是给定的Object实例

Catchable fatal error: Argument 1 passed to MyClass\Util\Sample.php must be an instance of MyClass\Util\Object, instance of Object given

推荐答案

您可以使用\指向全局命名空间:

You can use \ to point to the global namespace :

namespace MyClass\Util;

class Sample {

  public function each(\Object $f) {

  }
}


作为参考,您可以阅读 全局空间 (引用) :


As a reference, you can read Global space (quoting) :

使用\前缀名称将指定 该名称是必填项 即使在以下情况下,全球空间 命名空间.

Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.

这篇关于如何在PHP 5.3+中的命名空间类中使用全局命名空间类型提示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆