- IIS中设置站点或目录的执行权限
- concat函数在js中的使用
- C#异步方法async/await的三种返回类型
- 经典SQL语句大全之技巧篇
- hreflang 标签
- js计算时间差
- SQL SERVER报错:没有足够的内存继续执行程序 (mscorlib)
- .Net中Math.Round与四舍五入
- 内部链接的优化
- 写网站Title时应该注意什么?
邮箱:
手机:15383239821
php中static关键字的作用
static关键字的作用如下:
1、放在函数内部修饰变量;
2、放在类里修饰属性或方法;
3、放在类的方法里修饰变量;
4、修饰全局作用域的变量;
关键字所表示的不同含义如下:
1、在函数执行完后,变量值仍然保存
<?php
function testStatic() {
static $val = 1;
echo $val;
$val++;
}
testStatic(); //output 1
testStatic(); //output 2
testStatic(); //output 3
?>
2、修饰属性或方法,可以通过类名访问,如果是修饰的是类的属性,保留值
<?php
class Person {
static $id = 0;
function __construct() {
self::$id++;
}
static function getId() {
return self::$id;
}
}
echo Person::$id; //output 0
echo "<br/>";
$p1=new Person();
$p2=new Person();
$p3=new Person();
echo Person::$id; //output 3
?>
3、修饰类的方法里面的变量
<?php
class 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
static $name = 1;
$name++;
echo $name;
?>
另外:考虑到PHP变量作用域
<?php
include 'ChromePhp.php';
$age=0;
$age++;
function test1() {
static $age = 100;
$age++;
ChromePhp::log($age); //output 101
}
function test2() {
static $age = 1000;
$age++;
ChromePhp::log($age); //output 1001
}
test1();
test2();
ChromePhp::log($age); //outpuut 1
?>
可以看出,这3个变量是不相互影响的。另外,PHP里面只有全局作用域和函数作用域,没有块作用域。
- 上一篇:PHP中=>和->有什么区别?
- 下一篇:thinkphp6执行流程
-
2023-04-28DTO类
-
2014-02-24标签列表(字母排序)
-
2024-07-16SKU 和 SPU 有什么区别?
-
2013-10-30RequiredFieldValidator空值检验点击返回时不想检验怎做
-
2011-03-19(int) 和int.Parse()的区别
-
2020-08-03SEO如何优化好网站?SEO有哪些优化网站的方法?
