这是我的来源:
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_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;
// ...
}
}
}
但它失败了。
我该怎么做。
答案 0 :(得分:2)
您可以将该属性定义为静态,但它不适合您,因为您在类变量定义期间无法调用方法
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;
}
}
如果你想要静态方法,那么你需要像自己的初始化方法那样初始化属性,但它有它的缺点。
/**
* 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 (::)
}
}
更好的选项可能是singleton pattern,这是类的实例,但只是一次,并且它是一些非常接近静态方法的方法(不是相同的)。
class TestSingleton {
/**
* Singleton instance
*
* @var TestSingleton
*/
private $instance = null;
/**
* My special config value
*/
private $share_var;
/**
* For singleton make construct private
*/
private function __construct() {
// You can now initialize this private variable in constructor
self::$share_var = Config::getVarA();
}
/**
* Returns instance of this class
*
* @return TestSingleton
*/
public static function getInstance() {
if (self::$instance === null) {
self::$instance = new self(); // also new TestSingleton() woudl work
}
// return the instance
return self::$instance;
}
/**
* See this method is not static anymore
*/
public function a() {
echo $this->share_var_static;
}
/**
* See this method is not static anymore
*/
public function b() {
echo $this->share_var_static;
}
}
//然后你将该方法称为:
TestSingleton::getInstance()->a();
TestSingleton::getInstance()->b();
// or
$myInstance = TestSingleton::getInstance();
$myInstance->a();
$myInstance->b();
下一个选项是使用普通的非静态方法和对象实例,并在构造函数中初始化对象属性,但我猜你知道怎么做。 我假设你想要更像静态的东西......
答案 1 :(得分:1)
您必须使用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();
答案 2 :(得分:-1)
class Test {
private $share_var = ClassConfig::getVarA();
public static function a() {
echo $this->share_var;
}
public static function b() {
echo $this->share_var;
}
}
如果您想了解有关静态关键字的更多信息,可以点击以下链接,其详细信息如下:static keyword