1 <html>
 2      <body>
 3          <!-- 在类中的使用 -->
 4          <?php
 5              class TestStatic {
 6                  public static $country = 'China';    //在类内static可以使用public修饰
 7                  
 8                  public function getCountry() {
 9                      return self::$country;    //类内调用static变量需要使用self
10                  }
11                  
12                  public static function getCountrySt() {
13                     return self::$country;
14                  }
15              }
16              
17              $test = new TestStatic();
18              echo $test->getCountry() . '<br>';
19              echo TestStatic::getCountrySt() . '<br>';    //可以直接使用class::staticFunc的形式调用static方法
20              echo TestStatic::$country . '<br>';    //可以直接使用class::staticVar的形式调用static变量
21          ?>
22          
23          <!-- 在脚本中的使用 -->
24          <?php
25              static $country = 'Japan';    //脚本中的变量不能用public修饰
26              echo $country . '<br>';
27          ?>
28      </body>
29  </html>

页面输出

China
China
China
Japan

posted on 2012-05-04 01:49  ZimZz  阅读(288)  评论(0编辑  收藏  举报