如何在PHP中定义静态属性?

如何在PHP中定义静态属性?,php,Php,以下是我的资料来源: class Test { public static function a() { $share_var = ClassConfig::getVarA(); // return a hardcoding const 10; // echo $share_var; // ... } } public static function b() {

以下是我的资料来源:

class Test {
    public static function a() {
            $share_var = ClassConfig::getVarA(); // return a hardcoding const 10;
            // echo $share_var;
            // ...
            }
    }
    public static function b() {
            $share_var = ClassConfig::getVarA(); // return a hardcoding const 10;
            // echo $share_var;
            // ...
            }
    }
}   
所以
$share\u var=ClassConfig::getVarA()被调用两次。所以我做了这样的事情:

class Test {
    private static $share_var = ClassConfig::getVarA(); // return a hardcoding const 10;
    public static function a() {
            // echo $share_var;
            // ...
            }
    }
    public static function b() {
            // echo $share_var;
            // ...
            }
    }
}
但它失败了


我该怎么做。

您必须使用
self

class Test {
    private static $share_var = 'Something';

    public static function a() {
        echo self::$share_var;

    }
    public static function b() {
        echo self::$share_var;
    }
}

Test::a();
Test::b();

您可以将属性定义为静态,但您没有选择,因为在类变量定义期间,无法调用方法

class TestA {
    private static $share_var_private = 10; // You can do this, but you can't call another method here eg TestB::a();
    private static $share_var_private = Config::getA(); // This won't work


    public static function a() {
        echo self::$share_var_private;
    }
}
如果您想要静态方法,那么您需要像own initialize method这样的东西来初始化属性,但它有它的缺点

/**
 * This is example of what I've described, but it is not so good for usage because you have to call init() method before you can use the class. You could call init method in each class method, but you can imagine it wouldn't be nice.
 */
class TestWithInit {
    /**
     * When it's defined as static, it can't be defined as private in php
     */
    private static $share_var_static; // You can't call another class here eg TestB::a();

    public static function init() {
        self::$share_var_static = Config::getVarA();
    }

    public static function a() {
        echo self::$share_var_static;
    }

    public static function b() {
        echo self::$share_var_privat; // This would also work ... calling private variable as static (::)
    }
}
更好的选择可能是类的实例,但只有一次,而且在某些方面它非常接近静态方法(不同)

//然后调用该方法,如下所示:

TestSingleton::getInstance()->a();
TestSingleton::getInstance()->b();

// or
$myInstance = TestSingleton::getInstance();
$myInstance->a();
$myInstance->b();
下一个选项是使用普通的非静态方法和对象实例,并在构造函数中初始化对象属性,但我想你们知道怎么做。 我想你想要更像静电的东西

class Test {
      private $share_var = ClassConfig::getVarA();

      public static function a() {
         echo $this->share_var;

      }
      public static function b() {
        echo $this->share_var;
      }
}

如果您想了解更多关于静态关键字的信息,可以单击下面的链接,它的详细信息:

原始代码的一个问题是您回显的是
$share\u var
,而不是
self::$share\u var
。您可以将任何东西分配给<代码>私有静态$SysAlva,它不会在您的其他方法中回响,而不使用<代码>::$ SysYVAR 只是一个旁侧:如果您显然需要以某种方式配置您的类,然后考虑使其实例化,并将配置对象(与静态类相反)传递给构造函数,通过这种方式,您将能够处理类的多个不同配置<代码>类Nya{const nyu=“value”;公共函数nyaa(){echo nyu;}}
最好在答案中添加最相关的信息,而不是只给出链接。外部资源将来可能不可用。嘿,捷运,谢谢你提醒,我想我下次会做得更好。不,对不起。不能在静态函数中使用$this。
class Test {
      private $share_var = ClassConfig::getVarA();

      public static function a() {
         echo $this->share_var;

      }
      public static function b() {
        echo $this->share_var;
      }
}