首页 > 其他 > 详细

“延迟静态绑定”的使用

时间:2015-10-19 19:18:10      阅读:278      评论:0      收藏:0      [点我收藏+]
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <?php

class ParentBase {

  static $property = ‘Parent Value‘;

  public static function render() {

    return self::$property;

  }

}

class Descendant extends ParentBase {

  static $property = ‘Descendant Value‘;

}

echo Descendant::render();

Parent Value

在这个例子中,render()方法中使用了self关键字,这是指ParentBase类而不是指Descendant类。在ParentBase::render()方法中没法访问$property的最终值。为了解决这个问题,需要在子类中重写render()方法。

通过引入延迟静态绑定功能,可以使用static作用域关键字访问类的属性或者方法的最终值。

Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 <?php

class ParentBase {

  static $property = ‘Parent Value‘;

  public static function render() {

    return static::$property;

  }

}

class Descendant extends ParentBase {

  static $property = ‘Descendant Value‘;

}

echo Descendant::render();

Descendant Value

 

“延迟静态绑定”的使用

原文:http://www.cnblogs.com/mysic/p/4892403.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!