简体   繁体   中英

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";

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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