<转载>PHP的静态变量介绍

PHP的静态变量介绍

静态变量只存在于函数作用域内,也就是说,静态变量只存活在栈中。一般的函数内变量在函数结束后会释放,比如局部变量,但是静态变量却不会。就是说,下次再调用这个函数的时候,该变量的值会保留下来。
只要在变量前加上关键字static,该变量就成为静态变量了。

 1 <?php
 2     function test()
 3     {
 4 
 5         static $nm = 1;
 6 
 7         $nm = $nm * 2;
 8 
 9         print $nm."<br />";
10 
11     }
12      
13     // 第一次执行,$nm = 2
14     test();
15     // 第一次执行,$nm = 4
16     test();
17     // 第一次执行,$nm = 8
18     test();
19 ?>

 

程序运行结果:

2
4
8

 

函数test()执行后,变量$nm的值都保存了下来了。
在class中经常使用到静态属性,比如静态成员、静态方法。
Program List:类的静态成员
静态变量$nm属于类nowamagic,而不属于类的某个实例。这个变量对所有实例都有效。
::是作用域限定操作符,这里用的是self作用域,而不是$this作用域,$this作用域只表示类的当前实例,self::表示的是类本身。

 1 <?php
 2     class nowamagic
 3     {
 4         public static $nm = 1;
 5          
 6         function nmMethod()
 7         {
 8             self::$nm += 2;
 9             echo self::$nm . '<br />';
10         }
11     }
12      
13     $nmInstance1 = new nowamagic();
14     $nmInstance1 -> nmMethod();
15      
16     $nmInstance2 = new nowamagic();
17     $nmInstance2 -> nmMethod();
18 ?>

 

程序运行结果:

3
5

 

Program List:静态属性

 1 <?php
 2     class NowaMagic
 3     {
 4         public static $nm = 'www.nowamagic.net';
 5  
 6         public function nmMethod()
 7         {
 8             return self::$nm;
 9         }
10     }
11      
12     class Article extends NowaMagic
13     {
14         public function articleMethod()
15         {
16             return parent::$nm;
17         }
18     }
19      
20     // 通过作用于限定操作符访问静态变量
21     print NowaMagic::$nm . "<br />";
22      
23     // 调用类的方法
24     $nowamagic = new NowaMagic();
25     print $nowamagic->nmMethod() . "<br />";
26      
27     print Article::$nm . "<br />";
28      
29     $nmArticle = new Article();
30     print $nmArticle->nmMethod() . "<br />";
31 ?>

 

程序运行结果:

1 www.nowamagic.net
2 www.nowamagic.net
3 www.nowamagic.net
4 www.nowamagic.net

 

Program List:简单的静态构造器
PHP没有静态构造器,你可能需要初始化静态类,有一个很简单的方法,在类定义后面直接调用类的Demonstration()方法。

 1 <?php
 2 function Demonstration()
 3 {
 4     return 'This is the result of demonstration()';
 5 }
 6  
 7 class MyStaticClass
 8 {
 9     //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error
10     public static $MyStaticVar = null;
11  
12     public static function MyStaticInit()
13     {
14         //this is the static constructor
15         //because in a function, everything is allowed, including initializing using other functions
16          
17         self::$MyStaticVar = Demonstration();
18     }
19 } MyStaticClass::MyStaticInit(); //Call the static constructor
20  
21 echo MyStaticClass::$MyStaticVar;
22 //This is the result of demonstration()
23 ?>

 

程序运行结果:

1 This is the result of demonstration()

 

转自:http://www.nowamagic.net/php/php_StaticVariable.php

转载于:https://www.cnblogs.com/gordon-ge/archive/2012/08/02/2619640.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值