Private static variables in php class

本秂侑毒 提交于 2020-01-13 08:33:32

问题


I have a few classes that are often run through var_dump or print_r.

Inside these classes I have a few variables that are references to other, rather large objects that only ever has one instance of each and are only used inside the classes (outside the classes have their own reference to these classes) I do not wish these classes printed in the output, so I have declared them as private static which is working fine.

But my IDE (PHPstorm) is flicking up an error-level alert with Member has private access when I access them through self::$ci->...

I am wondering if this is a bug in the IDE, highlighting because it's probably a bug (aka it's static but nothing outside the class can access it, why would you want to to do that?), or because there is actually something syntactically wrong with it?

As an example here is part of the class, Note that =& get_instance(); returns a reference to the Code Igniter super object

private static $ci = null;

public function __construct(){
    self::$ci = self::$ci =& get_instance();
}

public function product() {
    if ($this->product == null) {
        self::$ci->products->around($this->relative_date);
        $this->product = self::$ci->products->get($this->product_id);
    }
    return $this->product;
}

回答1:


In your product() method you're trying to access the private member self::$ci. Your IDE thinks that this method can be accessed anywhere, and detects a conflict with the private static member $ci.



来源:https://stackoverflow.com/questions/13634712/private-static-variables-in-php-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!