PHP static 关键字
示例
创建和使用静态属性和方法
<?php
class MyClass {
public static $str = "Hello World!";
public static function hello() {
echo MyClass::$str;
}
}
echo MyClass::$str;
echo "<br>";
echo MyClass::hello();
?>
自己尝试 »
定义和使用
The static
keyword is used to declare properties and methods of a class as static. Static properties and methods can be used without creating an instance of the class.
The static
keyword is also used to declare variables in a function which keep their value after the function has ended.
相关页面
阅读更多关于静态方法的教程:PHP OOP - 静态方法教程.
阅读更多关于静态属性的教程:PHP OOP - 静态属性教程.
更多示例
示例
在函数中使用静态变量
<?php
function add1() {
static $number = 0;
$number++;
return $number;
}
echo add1();
echo "<br>";
echo add1();
echo "<br>";
echo add1();
?>
自己尝试 »
❮ PHP 关键字