PHP – 靜態方法



PHP 中的“static”關鍵字用於在 PHP 類中定義靜態屬性和靜態方法。需要注意的是,static 關鍵字也用於定義靜態變數和靜態匿名函式。本章討論 PHP 類中的靜態方法。

在類定義中,使用 static 修飾符宣告的函式成為其靜態方法。

class myclass {
   public static function myStaticMethod() {
      // ...
}

無需建立類的例項即可呼叫其靜態方法。靜態方法透過類名和作用域解析運算子來呼叫。靜態方法呼叫的語法為:

myclass::myStaticMethod();

由於靜態方法無需建立類的例項即可呼叫,因此在靜態方法內部無法使用偽變數 $this。允許透過物件呼叫靜態方法,儘管將例項方法作為靜態方法呼叫會引發錯誤。

示例

請看下面的例子:

<?php
   class myclass {
   
      /* Member variables */
      static int $var1 = 0;
      public static function mystaticmethod() {
         echo "This is a static method".  PHP_EOL;
      }
      public function myinstancemethod() {
         echo "This is an instance method".  PHP_EOL;
      }
   }

   myclass::mystaticmethod();
   $obj = new myclass;
   $obj->myinstancemethod();
   $obj->mystaticmethod();
   myclass::myinstancemethod();
?>

它將產生以下輸出

This is a static method
This is an instance method
This is a static method
PHP Fatal error:  Uncaught Error: Non-static method 
myclass::myinstancemethod() cannot be called statically

靜態方法中的“self”關鍵字

如果需要在同一類中定義的例項方法內部呼叫靜態方法,則必須使用 self 關鍵字引用類名,後跟作用域解析運算子(例如 self::mystaticmethod)

<?php
   class myclass {
   
      /* Member variables */
      static int $var1 = 0;
      public static function mystaticmethod() {
         echo "This is a static method".  PHP_EOL;
      }
      public function myinstancemethod() {
         echo "This is an instance method".  PHP_EOL;  
         echo "calling static method from instance method" . PHP_EOL;
         self::mystaticmethod();
      }
   }

   $obj = new myclass;
   $obj->myinstancemethod();
?>

它將產生以下輸出

This is an instance method
calling static method from instance method
This is a static method

使用“parent”關鍵字

在繼承的情況下,可以透過物件(派生類物件)或透過派生類的例項方法內部呼叫基類中定義的靜態方法,方法是使用“parent”關鍵字引用它。

示例

請看下面的例子:

<?php
   class myclass {
   
      /* Member variables */
      static int $var1 = 0;
      public static function mystaticmethod() {
         echo "This is a static method".  PHP_EOL;
      }
      public function myinstancemethod() {
         echo "This is an instance method".  PHP_EOL;
         echo "calling static method from instance method" . PHP_EOL;
         self::mystaticmethod();
      }
   }

   class mynewclass extends myclass {
      public function myfunction() {
         echo "This an instance method of the derived class" . PHP_EOL;
         echo "Calling static method of the parent class" . PHP_EOL;
         parent::mystaticmethod();
      }
   }
   $obj = new mynewclass;
   mynewclass::mystaticmethod();
   $obj->myfunction();
?>

它將產生以下輸出

This is a static method
This an instance method of the derived class
Calling static method of the parent class
This is a static method

另一個類中的靜態方法

完全可以從一個類中呼叫另一個類中的靜態方法。必須使用其類名字首加上作用域解析運算子限定其名稱。

示例

請看下面的例子:

<?php
   class myclass {
   
      /* Member variables */
      static int $var1 = 0;
      public static function mystaticmethod() {
         echo "This is a static method".  PHP_EOL;
      }
   }
   
   #this is not a derived class
   class mynewclass {
      public function myfunction() {
         echo "This an instance method" . PHP_EOL;
         echo "Calling static method of the another class" . PHP_EOL;
         myclass::mystaticmethod();
      }
   }
   $obj = new mynewclass;
   $obj->myfunction();
?>

它將產生以下輸出

This an instance method
Calling static method of another class
This is a static method

由於$this偽變數在靜態方法中不可用,因此無法在靜態方法內部訪問物件的例項變數。它只能處理類的靜態屬性。

示例

請看下面的例子:

<?php
   class myclass {
   
      /* Member variables */
      static int $var1 = 0;
      function __construct() {
         self::$var1++;
         echo "object number ". self::$var1 . PHP_EOL;      
      }
      public static function mystaticmethod() {
         echo "Number of objects available: " . self::$var1 . PHP_EOL;
      }
   }

   for ($i=1; $i<=3; $i++) {
      $obj = new myclass;
   }
   myclass::mystaticmethod();
?>

它將產生以下輸出

object number 1
object number 2
object number 3
Number of objects available: 3
廣告