靜態屬性有兩種,局部靜態跟全局靜態,局部靜態是只在函數作用域內有用,外部引用時會報錯,全局靜態則在函數內外部都可以引用且改變值。靜態屬性只會初始化一次,然后就會存在一塊固定的內存塊里。function test(){ static $var = 5; //static $var = 1+1;就會報錯 $var++; echo $var . ' ';}test(); //2test(); //3test(); //4echo $var; //報錯:Notice: Undefined variable: varfunction static_global(){ global $glo; $glo++; echo $glo.'<br>';}static_global(); //1static_global(); //2static_global(); //3echo $glo . '<br>'; //3關于靜態方法與非靜態方法之間調用的注意事項,下面舉例說明:class Banji{ public static $nums = 0; public $name = ''; public function __construct($name){ $this->name = $name; self::$nums++; } public function nums(){ echo self::$nums; echo '<br>'; echo $this->name; echo '<br>'; } public static function test(){ echo self::$nums; echo '<br>'; } public static function test1(){ echo self::$nums; echo '<br>'; echo $this->name; echo '<br>'; } public static function test2(){ self::nums(); } public static function test3(){ $this->nums(); } public function test4(){ self::test(); }}$student = new Banji('Susunma');$student->nums(); //1 Susunma$student = new Banji('Susunma1');$student->nums(); //2 Susunma1$student = new Banji('Susunma2');$student->nums(); //3 Susunma2$student->test(); //3$student->test1(); //Fatal error: Uncaught Error: Using $this when not in object context 即靜態方法中不能調用非靜態屬性Banji::test(); //3Banji::test1(); //Fatal error: Uncaught Error: Using $this when not in object context$student->test2(); //DePRecated: Non-static method Banji::nums() should not be called statically 非靜態方法中不能以靜態方式調用$student->test3(); //Fatal error: Uncaught Error: Using $this when not in object context 即靜態方法中不能調用非靜態方法$student->test4(); //3 即非靜態方法可以訪問靜態方法以及靜態屬性