Home > Backend Development > PHP Tutorial > static变量生命周期 在php和java中的不同

static变量生命周期 在php和java中的不同

WBOY
Release: 2016-06-23 13:27:25
Original
1355 people have browsed it

<%!  static class Count {      private static int count = 0;      public static int getCount(){          count++;          return count;      }  }  %>  <%   out.print(Count.getCount());  %>  
Copy after login

  通过浏览器连续访问,会分别输出:1,2,3,4,5,6,........

<?php  class Count{      private static $count = 0;      public static function getCount(){          self::$count++;          return self::$count;      }  }  echo Count::getCount();  ?>  
Copy after login

  通过浏览器连续访问,会分别输出:1,1,1,1,1,1,........

总结

1、java的static变量伴随着java虚拟机的退出而消亡,java虚拟机运行期间,static变量一直存在。

2、php的static变量只针对一次请求(一次php文件的执行),php文件执行完毕,该static变量也随机消亡,再次请求(再次执行该php文件),会重新创建该static变量。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template