• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php方法中的静态变量是什么

php 搞代码 4年前 (2022-01-04) 21次浏览 已收录 0个评论

php中的静态变量只存在于函数作用域内,也就是说,静态变量只存活在栈中;在PHP中只要在变量前加上关键字static,该变量就成为静态变量了。

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php方法中的静态变量是什么?

PHP中的静态变量及static静态变量使用详解

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

只要在变量前加上关键字static,该变量就成为静态变量了。

<?php
  function test()
  {
    static $nm = ;
    $nm = $nm * ;
    print $nm."<br />";
  }
  // 第一次执行,$nm = 
  test();
  // 第一次执行,$nm = 
  test();
  // 第一次执行,$nm = 
  test();
?>

程序运行结果:
1
2
2
4
3
8

函数test()执行后,变量$nm的值都保存了下来了。

在class中经常使用到静态属性,比如静态成员、静态方法。

Program List:类的静态成员

静态变量$nm属于类nowamagic,而不属于类的某个实例。这个变量对所有实例都有效。

::是作用域限定操作符,这里用的是self作用域,而不是$this作用域,$this作用域只表示类的当前实例,self::表示的是类本身。

<?php
  class nowamagic
  {
    public static $nm = ;
    function nmMethod()
    {
      self::$nm += ;
      echo self::$nm . '<br />';
    }
  }
  $nmInstance = new nowamagic();
  $nmInstance -> nmMethod();
  $nmInstance = new nowamagic();
  $nmInstance -> nmMethod();
?>

程序运行结果:
1
3
2
5

Program List:静态属性

<?php
  class NowaMagic
  {
    public static $nm = 'www.nowamagic.net';
    public function nmMethod()
    {
      return self::$nm;
    }
  }
  class Article extends NowaMagic
  {
    public function articleMethod()
    {
      return parent::$nm;
    }
  }
  // 通过作用于限定操作符访问静态变量
  print NowaMagic::$nm . "<br />";
  // 调用类的方法
  $nowamagic = new NowaMagic();
  print $nowamagic->nmMethod() . "<br />";
  print Article::$nm . "<br />";
  $nmArticle = new Article();
  print $nmArticle->nmMethod() . "<br />";
?>

程序运行结果:

http://www.nowamagic.net
http://www.nowamagic.net
http://www.nowamagic.net
http://www.nowamagic.net

Program Lis来源gao@dai!ma.com搞$代^码网t:简单的静态构造器

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

<?php
function Demonstration()
{
  return 'This is the result of demonstration()';
}
class MyStaticClass
{
  //public static $MyStaticVar = Demonstration(); //!!! FAILS: syntax error
  public static $MyStaticVar = null;
  public static function MyStaticInit()
  {
    //this is the static constructor
    //because in a function, everything is allowed, including initializing using other functions
    self::$MyStaticVar = Demonstration();
  }
} MyStaticClass::MyStaticInit(); //Call the static constructor
echo MyStaticClass::$MyStaticVar;
//This is the result of demonstration()
?>

程序运行结果:

This is the result of demonstration()

下面给大家介绍PHP静态变量static的使用介绍

static关键字在C#编程中非常常见,它用来修饰符声明属于类型本身而不是属于特定对象的静态成员。static 修饰符可用于类、字段、方法、属性、运算符、事件和构造函数,但不能用于索引器、析构函数或类以外的类型。声明为static的类、函数和变量将不能引用实例方法或变量,另外在C#中一旦类被添加了static修饰符,则其内部所有变量和方法都必须是静态的。静态变量和方法必须通过类名进行引用而不能通过实例对象引用。


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php方法中的静态变量是什么

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址