简体   繁体   English

如何在PHP 5.3+中的命名空间类中使用全局命名空间类型提示?

[英]How do I use global namespace type hinting inside of a namespaced class in 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());

Produces: 生产:

Catchable fatal error: Argument 1 passed to MyClass\\Util\\Sample.php must be an instance of MyClass\\Util\\Object, instance of Object given 可捕获的致命错误:传递给MyClass \\ Util \\ Sample.php的参数1必须是MyClass \\ Util \\ Object的实例,给出的O​​bject实例

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. 使用\\前缀名称将指定即使在命名空间的上下文中也需要全局空间中的名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM