douxiuar885064 2013-11-03 05:12
浏览 39

PHP中的特征是否受命名空间的影响?

From the PHP documentation:

only four types of code are affected by namespaces: classes, interfaces, functions and constants.

But, it seems to me that TRAITS are also affected:

namespace FOO;

trait fooFoo {}

namespace BAR;

class baz
{
    use fooFoo; // Fatal error: Trait 'BAR\fooFoo' not found in
}

Am I wrong?

  • 写回答

3条回答 默认 最新

  • du3979 2013-11-03 05:51
    关注

    I think they are affected as well. Look at some of the comments on the php.net page.

    The first comment:

    Note that the "use" operator for traits (inside a class) and the "use" operator for namespaces (outside the class) resolve names differently. "use" for namespaces always sees its arguments as absolute (starting at the global namespace):
    
    <?php
    namespace Foo\Bar;
    use Foo\Test;  // means \Foo\Test - the initial \ is optional
    ?>
    
    On the other hand, "use" for traits respects the current namespace:
    
    <?php
    namespace Foo\Bar;
    class SomeClass {
        use Foo\Test;   // means \Foo\Bar\Foo\Test
    }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?