PHP之static静态变量
来源:互联网 发布:淘宝活动第三方平台 编辑:程序博客网 时间:2024/05/17 03:41
在php中static用法如下:
1.static 放在函数内部修饰变量
2.static放在类里修饰属性,或方法
3.static放在类的方法里修饰变量
4.static修饰在全局作用域的变量
1.在函数执行完后,变量值仍然保存
<?phpfunction testStatic() { static $val = 1; echo $val; $val++;}testStatic(); //output 1testStatic(); //output 2testStatic(); //output 3?>
2.修饰属性或方法,可以通过类名访问,如果是修饰的是类的属性,保留值
<?phpclass Person { static $id = 0; function __construct() { self::$id++; } static function getId() { return self::$id; }}echo Person::$id; //output 0echo "<br/>";$p1=new Person();$p2=new Person();$p3=new Person();echo Person::$id; //output 3?>
3.修饰类的方法里面的变量
<?phpclass Person { static function tellAge() { static $age = 0; $age++; echo "The age is:".$age; }}echo Person::tellAge(); //output 'The age is: 1'echo Person::tellAge(); //output 'The age is: 2'echo Person::tellAge(); //output 'The age is: 3'echo Person::tellAge(); //output 'The age is: 4'?>
4.修饰全局作用域的变量,没有实际意义(存在着作用域的问题,详情查看)
<?php $age=0; $age++; function test1() { static $age = 100; $age++; } function test2() { static $age = 1000; $age++; }test1();//output 101test2();//output 1001?>
阅读全文
0 0
- PHP之static静态变量
- php static 静态变量
- PHP静态变量 static
- PHP static静态变量
- 【PHP】static静态变量
- PHP 静态变量Static
- PHP static静态变量
- php 静态变量static相关
- PHP static之静态变量和属性方法
- PHP之static静态变量详解(一)
- PHP之static静态变量详解(二)
- PHP之static静态变量详解(二)
- PHP之static静态变量详解(一)
- PHP之static静态变量详解(一)
- PHP之static静态变量详解(二)
- 【PHP】static静态变量,静态方法
- PHP类的静态(static)方法和静态(static)变量
- PHP static局部静态变量和全局静态变量总结
- [编程题] 独立的小易
- C++入门(作为大概了解)
- python 循环
- iOS NSdata 与 NSString,Byte数组,UIImage 的相互转换
- 一个继承JFrame的类调用一个继承JPanel的类,继承JPanel中的定义JPanel不显示
- PHP之static静态变量
- RIP颜色混合算法
- 4.Redis的数据库
- 14jQuery操作DOM、事件
- Ngrok实现远程控制和操作树莓派(Raspbian系统)
- Spring AOP 总结
- HDU 6118 度度熊的交易计划 (最小费用最大流模板题)
- 理解MySQL——索引与优化
- Java的位运算符详解实例——与(&)、非(~)、或(|)、异或(^)