简体   繁体   English

从另一个类中引用静态类

[英]Referencing a static class from within another class

I have a class: 我有一堂课:

class Site 
{
    public static $url;
}

I assign a value to it: Site::$url = "whatever"; 我给它赋一个值:Site :: $ url =“ whatever”;

I have another class: 我还有另一堂课:

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

How do I give scope to the Something class. 如何将范围赋予Something类。

It should work as you've written it above: 如您在上面所写,它应该可以工作:

class Site {
  public static $url;
}

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

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

If that's not working, it's likely because the classes are being declared in two different namespaces. 如果这不起作用,则可能是因为在两个不同的命名空间中声明了类。

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

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