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
广告
© . All rights reserved.