简体   繁体   中英

How to reference a static class variable inside a namespace?

Inside my php class Patient, I have a static method which init's some static class variables, which works fine. I do so like this:

private static $request;

public static function init() {
    self::$request = new Something();
}

But when I add a namespace to the top of the file, it dies on this line for some reason, and it's confusing me. Is there a special way to call static variables when inside a namespace?

Since your code works when it's in the global namespace, but fails when it's in a custom namespace, you'll need to provide a fully-qualified class name whenever you use external classes. So in this situation, simply change your code to:

public static function init() {
    self::$request = new \Something();
}

And if for some reason this doesn't solve your issue, then you can at least rule out static variables as being related to the problem by (temporarily) changing self::$request = to $temp = and observing that the problem still occurs.

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