* 范围解析符::
* 一、用途
* 1.调用静态成员(类外部)
* 2.调用类常量
* 3.调用被子类覆盖的类成员(属性/方法)
* 二、场景
* 1.类外部:前面仅允许通过类名来访问类中成员
* 2.类内部:前面仅允许: self, parent,static
* 三、特殊: 允许使用类名变量来引用类名(php5.3+)class Demo1
{
const HOME = '合肥';
public static function test1()
{
return __METHOD__;
}
public static function test2()
{
//延迟静态绑定:后面会说到
//static::根据上下文来决定使用哪个类来调用
return static::test1();
}
}
class Demo2 extends Demo1
{
public static $name = '朱老师';
public static function test1()
{
//调用本类静态成员: self::
$res = self::$name.'
';
//调用父类静态成员属性: parent::
$res .= parent::HOME.'
';
//调用父类静态成员方法
$res .= parent::test1().'
';
$res .= __METHOD__;
return $res;
}
}
//1.类外部调用类静态成员echo Demo1::HOME,'
';
echo Demo1::test1();
echo '
';
echo Demo2::test1();
echo '
';
//注意Demo1调用test2()和Demo2调用test2()的区别
//Demo1::test()返回Demo1中的test1()运行结果echo Demo1::test2();
echo '
';
//Demo2::test2()返回Demo2中的test1()运行结果echo Demo2::test2();
echo '
';
//php5.3+,允许使用类变量来调用$class = 'Demo2';
echo $class::test2();