械馅摆瓣摊簇娶期显拇缺岛翠
范围解析符::
一、用途
1.调用静态成员(类外部)
2.调用类常量
3.调用被子类覆盖的类成员(属性/方法)
二、场景
1.类外部:前面仅允许通过类名来访问类中成员
2.类内部:前面仅允许: self, parent,static
三、特殊: 允许使用类名变量来引用类名(php5.3+)
新建demo4.php
<?php class Demo1 { const HOME = '重庆'; public static function test1() { return __METHOD__;//回当前的方法名,会加上类名 } public static function test2() { //调用当前类的静态方法 return static::test1(); } } class Demo2 extends Demo1 { public static $name="萌面人"; public static function test1() { //获取当前的静态属性 self指的是当前的类:Demo2 $res=self::$name; // 还可以写作: // $res=Demo2::$name; return $res; } } //1.类外部调用类静态成员 echo Demo1::HOME."<br>"; echo Demo1::test1()."<br>"; echo Demo1::test2()."<br>"; echo "<hr>"; echo Demo2::test1(); //根据调用者的不同返回不同的值 echo "<hr>"; //调用父类中test2()方法 test2()再调用Demo1中的test1()方法 echo Demo2::test2()."<br>"; //调用Demo1中的test2()方法 echo Demo1::test2()."<br>"; //php5.3+,允许使用类变量来调用 echo "<hr>"; $class="Demo2"; echo $class::test2()."<br>";
执行:
剩感递咐仕坡篙负猎黔眉谦宿