简体   繁体   English

静态私有变量在它的类 php 中的可见性

[英]visibility of the static private variable in it's class php

I have the following code:我有以下代码:

myClass extends Class{

    private static $var1 = 0;

    public function index(){
        if(some condition){
            $this->var1 = 1;
        }
    }

    public function success(){
        if($this->var1 == 0){
            ...
        }else{
            ...
        }
    }

}
?>

My problem is that I cannot access the var1 from the functions in the class.我的问题是我无法从类中的函数访问 var1。 What am I doing wrong here?我在这里做错了什么?

Static variables (and methods) are accessed using the :: operator.使用::运算符访问静态变量(和方法)。 To access $var1 within the class, use the following code instead:要在类中访问$var1 ,请改用以下代码:

myClass extends Class{

private static $var1 = 0;

public function index(){

    if(some condition){
        static::$var1 = 1;
   }
}

public function success(){
    if(static::$var1 == 0){
     ...
    }else{
     ...
    }
} 

}

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

相关问题 PHP:类变量可见性 - PHP: class variable visibility 在私有类方法中定义的php闭包中的静态变量 - static variable in php closure defined within a private class method php oop可见性:父类中的函数如何调用其自身的私有函数和子类的公共函数 - php oop visibility: How the function in parent class calls it's own private function and child class public function PHP和Java:将属性的可见性设置为私有,并通过派生类的过度杀手来访问它吗? - PHP & Java: Is setting property's visibility private and accessing it via getter from derived class overkill? php类中的私有静态变量 - Private static variables in php class PHP静态方法可以合法地具有受保护或私有的可见性吗? - Can PHP static methods legally have a visibility of protected or private? 哪个在PHP,静态变量或私有变量中更好? - Which is better in PHP, Static Variable or Private Variable? php中的受保护和私有可见性 - Protected and private visibility in php 使用私有可见性时,成员的PHP类Heritage和Iteration - PHP class Heritage and Iteration on members when using private visibility PHP:将未声明的类属性的默认可见性更改为protected / private - PHP: Changing default visibility of undeclared class properties to protected/private
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM