简体   繁体   English

php类中的私有静态变量

[英]Private static variables in php class

I have a few classes that are often run through var_dump or print_r . 我有一些经常通过var_dumpprint_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. 在这些类中,我有一些变量,它们是对其他大型对象的引用,每个对象只有一个实例,并且只在类中使用(类外部有自己对这些类的引用)我不希望这些类打印在输出中,所以我声明它们是private static ,工作正常。

But my IDE (PHPstorm) is flicking up an error-level alert with Member has private access when I access them through self::$ci->... 但我的IDE(PHPstorm)正在点击一个错误级别的警报,当我通过self::$ci->...访问它时, Member has private access 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? 我想知道这是否是IDE中的一个错误,突出显示因为它可能是一个错误(也就是它是静态的,但是类之外没有任何东西可以访问它,你为什么要这样做?),或者因为实际上存在语法上的错误用它?

As an example here is part of the class, Note that =& get_instance(); 这里的一个例子是类的一部分,注意=& get_instance(); returns a reference to the Code Igniter super object 返回对Code Igniter超级对象的引用

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

In your product() method you're trying to access the private member self::$ci . 在您的product()方法中,您尝试访问私有成员self::$ci Your IDE thinks that this method can be accessed anywhere, and detects a conflict with the private static member $ci . 您的IDE认为可以在任何地方访问此方法,并检测到与私有静态成员$ci的冲突。

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

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