繁体   English   中英

从另一个类中引用静态类

[英]Referencing a static class from within another class

我有一堂课:

class Site 
{
    public static $url;
}

我给它赋一个值:Site :: $ url =“ whatever”;

我还有另一堂课:

class Something
{
    public function __Construct()
    {
        echo Site::$url; // does not seem to have scope?
    }
}

如何将范围赋予Something类。

如您在上面所写,它应该可以工作:

class Site {
  public static $url;
}

class Something {
  public function __construct() {
    echo Site::$url;
  }
}

Site::$url = 'whatever';
new Something(); // prints 'whatever'

如果这不起作用,则可能是因为在两个不同的命名空间中声明了类。

暂无
暂无

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

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