Php 简明教程
PHP – Static Properties
PHP 中的“static”关键字用于在 PHP 类中定义静态属性和静态方法。可能注意的是,static 关键字也用于定义静态变量和静态匿名函数。阅读本章以了解 PHP 类中的静态属性。
The "static" keyword in PHP is used to define static properties and static methods in a PHP class. It may be noted that the static keyword is also used to define static variable, and static anonymous functions. Read this chapter to learn about the static properties in a PHP class.
在类定义中,用静态限定声明的变量将成为其静态属性。static 关键字可以在访问修饰符之前或之后出现。
In a class definition, a variable declared with a static qualifier becomes its static property. The static keyword may appear before or after the access modifier.
static private $var1;
public static $var2;
如果您想使用类型提示,该类型一定不能在 static 关键字之前。
If you want to use type hints, the type must not be before the static keyword.
static private string $var1;
public static float $var2;
静态属性在类中的值无法通过其对象(使用 → 运算符)访问。这样做会产生一条指出 Accessing static property myclass::$var1 as non static 的通知。相反,可以使用由“::”符号表示的范围解析运算符访问静态属性。
The value of the static property in a class is not accessible by its object (with the → operator). Doing so will result in a notice stating Accessing static property myclass::$var1 as non static. Instead, the static properties are accessed using the Scope Resolution Operator represented by the "::" symbol.
Example
请看以下示例:
Take a look at the following example −
<?php
class myclass {
static string $var1 = "My Class";
function __construct() {
echo "New object declared" . PHP_EOL;
}
}
$obj = new myclass;
echo "accessing static property with scope resolution operator: " . myclass::$var1 . PHP_EOL;
echo "accessing static property with -> operator: ". $obj->var1 . PHP_EOL;
?>
它将生成以下 output −
It will produce the following output −
New object declared
accessing static property with scope resolution operator: My Class
PHP Notice: Accessing static property myclass::$var1 as non static in hello.php on line 14
The "self" Keyword
要从方法内部访问静态属性,请使用 self 关键字引用当前类。在以下示例中,该类具有一个整型静态属性,每当声明一个新对象时都会对其进行递增。
To access the static property from inside a method, refer to the current class with the self keyword. In the following example, the class has an integer static property, which is incremented every time a new object is declared.
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct(){
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
}
for ($i=1; $i<=3; $i++) {
$obj = new myclass;
}
?>
它将生成以下 output −
It will produce the following output −
object number 1
object number 2
object number 3
The "parent" Keyword
可以将基类的静态属性通过引用 parent 关键字用在继承类的函数中。您需要使用“parent::static_property”语法。
The static property of a base class can be used inside a function of the inherited class by referring to the base by parent keyword. You need to use the "parent::static_property" syntax.
Example
查看以下示例 −
Take look at the following example −
<?php
class myclass {
/* Member variables */
static int $var1 = 0;
function __construct() {
self::$var1++;
echo "object number ". self::$var1 . PHP_EOL;
}
}
class newclass extends myclass{
function getstatic() {
echo "Static property in parent class: " . parent::$var1 . PHP_EOL;
}
}
$obj = new newclass;
$obj->getstatic();
?>
它将生成以下 output −
It will produce the following output −
object number 1
Static property in parent class: 1