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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应