簡體   English   中英

如何在PHP中定義靜態屬性?

[英]How to define a static property in PHP?

這是我的資料來源:

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

但是失敗了。

我怎樣才能做到這一點。

您可以將屬性定義為靜態,但是這對您來說是沒有選擇的,因為您不能在類變量定義期間調用方法

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

如果需要靜態方法,則需要類似自己的initialize方法的方法,該方法將初始化屬性,但它有缺點。

/**
 * 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 (::)
    }
}

更好的選擇可能是單例模式 ,它是類的實例,但只有一次,並且在某種程度上與靜態方法非常接近(NOT SAME)。

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();

下一個選擇是使用常規的非靜態方法和對象實例,以及在構造函數中初始化object屬性,但我想您知道如何做。 我想你想要的東西更像是靜態的...

您必須使用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 Test {
      private $share_var = ClassConfig::getVarA();

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

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

如果您想了解有關靜態關鍵字的更多信息,可以單擊以下鏈接,其詳細信息: 靜態關鍵字

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM