• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php中的范围解析符

php 搞代码 3年前 (2022-01-22) 17次浏览 已收录 0个评论

* 范围解析符::

* 一、用途

* 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.'<br>';        //调用父类静态成员属性: parent::        $res .= parent::HOME.'<br>';        //调用父类静态成员方法        $res .= parent::test1().'<br>';        $res .= __METHOD__;        return $res;    }    }

//1.类外部调用类静态成员

echo Demo1::HOME,'<br>';echo Demo1::test1();echo '<hr>';echo Demo2::test1();echo '<hr>';

//注意Demo1调用test2()和Demo2调用test2()的区别

//Demo1::test()返回Demo1中的test1()运行结果

echo Demo1::test2();echo '<hr>';

//Demo2::test2()返回Demo2中的test1()运行结果

echo Demo2::test2();echo '<hr><em>¥本文来%源[email protected]搞@^&代*@码)网5</em><strong>搞gaodaima代码</strong>';

//php5.3+,允许使用类变量来调用

$class = 'Demo2';echo $class::test2();

以上就是php中的范围解析符的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php中的范围解析符

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址