zoukankan      html  css  js  c++  java
  • “延迟静态绑定”的使用

    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
  • 相关阅读:
    linux 文件权限(s、t、i、a)解析
    vim Vundle
    数据结构学习(1)
    Android ImageView设置图片原理(上)
    C++11 之auto
    Android屏幕分辨率获取方法--源码剖析
    C++的发展方向是对的嘛?
    c++ 的前世今生
    学习知识的一种思路
    遗失的访谈小评
  • 原文地址:https://www.cnblogs.com/mysic/p/4892403.html
Copyright © 2011-2022 走看看