dtxf759200 2014-02-24 15:38
浏览 37
已采纳

YII中的父静态声明问题

Suppose I have this classes

<?php
class Grandparent {
    public function myMethod($param) {
        //do something
    }
}
class Parent extends Grandparent {
    public function myMethod($param) {
        //do something
        return parent::myMethod($param) ;
    }
}
class Son extents Parent {
    public function myMethod($param) {
        //do something
        //code to access parent parent
    }
}
?>

I want in myMethod of Son class to avoid call his parent and access to this grandparent. The code I tried was parent::parent::myMethod($param) but and the Ide throws me an error. Which is the way to access in a static way to grandparent with out passing through parent?

Is there any posibilities? I know there are some methods like get_parent_class, but I dont want them to use it. I need to call in static way.

  • 写回答

1条回答 默认 最新

  • douliang9057 2014-02-24 16:25
    关注

    http://www.php.net/manual/ru/language.oop5.inheritance.php

    <?php
    class foo
    {
      public function something()
      {
        echo __CLASS__; // foo
        var_dump($this);
      }
    }
    
    class foo_bar extends foo
    {
      public function something()
      {
        echo __CLASS__; // foo_bar
        var_dump($this);
      }
    }
    
    class foo_bar_baz extends foo_bar
    {
      public function something()
      {
        echo __CLASS__; // foo_bar_baz
        var_dump($this);
      }
    
      public function call()
      {
        echo self::something(); // self
        echo parent::something(); // parent
        echo foo::something(); // grandparent
      }
    }
    
    error_reporting(-1);
    
    $obj = new foo_bar_baz();
    $obj->call();
    
    // Output similar to:
    // foo_bar_baz
    // object(foo_bar_baz)[1]
    // foo_bar
    // object(foo_bar_baz)[1]
    // foo
    // object(foo_bar_baz)[1]
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 经gamit解算的cors站数据再经globk网平差得到的坐标做形变分析
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式