ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# static > 不需要实例化就可以直接调用 > 只存在于函数作用域的变量,函数执行之后变量的值不会丢失,只会初始化一次,初始化静态变量不能使用表达式,不用**全局变量**代替是因为全局变量会被所有函数访问容易造成维护不宜。 static修饰的属性和方法 类内: self::$属性名 self::方法名() 类外部: 类名::属性名 类名::方法名() const是常量 值不能修改了 static是静态成员 可以在不实例化得情况下 在类外直接访问 CONST不行 ``` <pre class="calibre10">``` class <span class="token4">demo</span><span class="token3">{</span> public static $name<span class="token1">=</span><span class="token2">'张三'</span><span class="token3">;</span> public static $login<span class="token1">=</span><span class="token6">0</span><span class="token3">;</span> public static <span class="token5">function</span> <span class="token4">son</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo self<span class="token3">:</span><span class="token3">:</span>$name<span class="token3">.</span><span class="token2">':'</span><span class="token3">.</span>self<span class="token3">:</span><span class="token3">:</span>$login<span class="token3">;</span> self<span class="token3">:</span><span class="token3">:</span>$login<span class="token1">++</span><span class="token3">;</span> self<span class="token3">:</span><span class="token3">:</span>$login<span class="token1">+</span><span class="token1">=</span><span class="token6">5</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> demo<span class="token3">:</span><span class="token3">:</span><span class="token4">son</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> demo<span class="token3">:</span><span class="token3">:</span><span class="token4">son</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> demo<span class="token3">:</span><span class="token3">:</span><span class="token4">son</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//输出 => 张三:0张三:6张三:12</span> ``` ``` > 静态方法中,$this伪变量不允许使用。可以使用self,parent,static在内部调用静态方法与属性: 静态方法也可以通过变量来进行动态调用: ``` <pre class="calibre10">``` class <span class="token4">Car</span> <span class="token3">{</span> private static $speed <span class="token1">=</span> <span class="token6">10</span><span class="token3">;</span> public static <span class="token5">function</span> <span class="token4">getSpeed</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> self<span class="token3">:</span><span class="token3">:</span>$speed<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $func <span class="token1">=</span> <span class="token2">'getSpeed'</span><span class="token3">;</span> $className <span class="token1">=</span> <span class="token2">'Car'</span><span class="token3">;</span> echo $className<span class="token3">:</span><span class="token3">:</span>$<span class="token4">func</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//动态调用静态方法相当于Car::getSpeed();</span> ``` ``` **作用**:static不管在类中还是函数中都会记住静态的值不会被销毁(一般函数执行完就销毁了) 1、不需要实例化就可以直接用self::静态方法名”或者“”类外可以使用“类名::静态方法名”调用; 2、静态属性、方法一个公用不管实例化多少次,静态方法都直接加载到内存的一个固定空间里,在内存中只有一份,为所有的实例共用;普通方法 你的方法调用需要先实例化类 然后 这部操作会在 内存中 另开辟空间 来存放这部分空间也省掉了~就是省资源 3、static 的属性和方法,只能访问static的属性和方法,不能类访问非静态的属性和方法。因为静态属性和方法被创建时,可能还没有任何这个类的实例可以被调用。 4、静态方法不能调用非静态属性。因为非静态属性需要实例化后,存放在对象里; 5、静态方法可以调用非静态方法,使用 self 关键词,一个方法被self:: 后,它就自动转变为静态方法; 什么情况下静态变量:在对象之间共享值时 方便访问变量时 ``` <pre class="calibre10">``` class <span class="token4">Person</span> <span class="token3">{</span> <span class="token">// 申明静态方法</span> public static <span class="token5">function</span> <span class="token4">demo</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> echo <span class="token2">'你好<br>'</span><span class="token3">;</span> <span class="token3">}</span> public <span class="token5">function</span> <span class="token4">demo1</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token">// 类内调用静态方法</span> self<span class="token3">:</span><span class="token3">:</span><span class="token4">demo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">// 在类外调用静态方法</span> Person<span class="token3">:</span><span class="token3">:</span><span class="token4">demo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//不实例化可以直接调用</span> $p <span class="token1">=</span> <span class="token5">new</span> <span class="token4">Person</span><span class="token3">;</span><span class="token">//实例化</span> <span class="token">// 调用demo1方法</span> $p<span class="token1">-</span><span class="token1">></span><span class="token4">demo1</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//你好</span> $p<span class="token1">-</span><span class="token1">></span><span class="token4">demo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//你好</span> <span class="token">//类外直接调用static</span> $p<span class="token3">:</span><span class="token3">:</span><span class="token4">demo</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//你好</span> ``` ``` 在类中使用static有两种主要用途、定义静态成员和定义静态方法。静态成员只保留一个变量的值,这个值对所有实例都是有效的,如下: ``` <pre class="calibre17">``` class <span class="token4">MyObject</span><span class="token3">{</span> public static $myStaticVar<span class="token1">=</span><span class="token6">0</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">myMethod</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> self<span class="token3">:</span><span class="token3">:</span>$myStaticVar<span class="token1">+</span><span class="token1">=</span><span class="token6">2</span><span class="token3">;</span> echo self<span class="token3">:</span><span class="token3">:</span>$myStaticVar<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $instance1<span class="token1">=</span><span class="token5">new</span> <span class="token4">MyObject</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//实例化一次 </span> $instance1<span class="token1">-</span><span class="token1">></span><span class="token4">myMethod</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $instance2<span class="token1">=</span><span class="token5">new</span> <span class="token4">MyObject</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span><span class="token">//再实例化 </span> $instance2<span class="token1">-</span><span class="token1">></span><span class="token4">myMethod</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token">//结果将分别打印2、4 </span> class <span class="token4">Person</span> <span class="token3">{</span> <span class="token">// 申明静态属性</span> public static $name <span class="token1">=</span> <span class="token2">'欧巴'</span><span class="token3">;</span> <span class="token">// self:自己</span> <span class="token">// 类内获取静态属性(self::属性名)</span> public <span class="token5">function</span> <span class="token4">getInfo</span><span class="token3">(</span><span class="token3">)</span> <span class="token3">{</span> <span class="token5">return</span> self<span class="token3">:</span><span class="token3">:</span>$name<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> <span class="token">// 实例化Person类</span> <span class="token">// $p = new Person;</span> <span class="token">// echo $p->name; // Accessing static property Person::$name as non static使用非静态方式获取静态属性</span> <span class="token">// 在类的外部获取静态属性(类名::属性名)</span> echo Person<span class="token3">:</span><span class="token3">:</span>$name<span class="token3">;</span> <span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span><span class="token1">--</span> class <span class="token4">test</span> <span class="token3">{</span> const constvar<span class="token1">=</span><span class="token2">'hello world'</span><span class="token3">;</span> static $staticvar<span class="token1">=</span><span class="token2">'hello world'</span><span class="token3">;</span> <span class="token5">function</span> <span class="token4">getStaticvar</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> <span class="token5">return</span> self<span class="token3">:</span><span class="token3">:</span>$staticvar<span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $obj<span class="token1">=</span><span class="token5">new</span> <span class="token4">test</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo test<span class="token3">:</span><span class="token3">:</span>constvar <span class="token">//输出'hello world' </span> echo test<span class="token3">:</span><span class="token3">:</span>staticvar <span class="token">//出错,staticvar 前必须加$才能访问,这是容易和类常量(per-class常量)容易混淆的地方之一 </span> echo test<span class="token3">:</span><span class="token3">:</span>$staticvar <span class="token">//输出'hello world' </span> $str<span class="token1">=</span><span class="token2">'test'</span><span class="token3">;</span> echo $str<span class="token3">:</span><span class="token3">:</span>$staticvar <span class="token">//出错,类名在这不能用变量动态化 </span> echo $str<span class="token3">:</span><span class="token3">:</span>constvar <span class="token">//出错原因同上 </span> <span class="token">//在类名称存在一个变量中处于不确定(动态)状态时,只能以以下方式访问类变量 </span> $obj2<span class="token1">=</span><span class="token5">new</span> $<span class="token4">str</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo $obj2<span class="token1">-</span><span class="token1">></span><span class="token4">getStaticvar</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> <span class="token1"><</span><span class="token1">?</span>php class <span class="token4">Book</span><span class="token3">{</span> static $num<span class="token1">=</span><span class="token6">0</span><span class="token3">;</span> public <span class="token5">function</span> <span class="token4">showMe</span><span class="token3">(</span><span class="token3">)</span><span class="token3">{</span> echo<span class="token2">"您是滴"</span><span class="token3">.</span>self<span class="token3">:</span><span class="token3">:</span>$num<span class="token3">.</span><span class="token2">"位访客"</span><span class="token3">;</span> self<span class="token3">:</span><span class="token3">:</span>$num<span class="token1">++</span><span class="token3">;</span> <span class="token3">}</span> <span class="token3">}</span> $book1<span class="token1">=</span><span class="token5">new</span> <span class="token4">Book</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $book1<span class="token1">-</span><span class="token1">></span><span class="token4">showMe</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo<span class="token2">"<br>"</span><span class="token3">;</span> $book2<span class="token1">=</span><span class="token5">new</span> <span class="token4">Book</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> $book2<span class="token1">-</span><span class="token1">></span><span class="token4">showMe</span><span class="token3">(</span><span class="token3">)</span><span class="token3">;</span> echo<span class="token2">"<br>"</span><span class="token3">;</span> echo<span class="token2">"您是滴"</span><span class="token3">.</span>Book<span class="token3">:</span><span class="token3">:</span>$num<span class="token3">.</span><span class="token2">"位访客"</span><span class="token3">;</span> <span class="token1">?</span><span class="token1">></span> ``` ```