dongyinshua9996 2015-04-14 16:00
浏览 36
已采纳

通过类层次结构递归调用方法

I want to call recursively the parent methods :

<?php
class generation1 {
    public function whoami() {
        echo get_class($this).PHP_EOL;
    }
    public function awesome() { // stop recursion
        $this->whoami();
    }
}

class generation2 extends generation1 {
    public function awesome() {
        $this->whoami();
        parent::awesome();
    }
}

class generation3 extends generation2 {}
class generation4 extends generation3 {}

$gen = new generation4();
$gen->awesome();

The output is :

generation4

generation4

I would like to have :

generation4

generation3

generation2

generation1

  • 写回答

1条回答 默认 最新

  • dtpwra8456 2015-04-14 16:08
    关注

    The __CLASS__ magic constant is not interpreted as being whatever the concrete class an instance is, but instead evaluates to the class name that the constant is contained in.

    If you want to get the concrete class name (rather than the base class name), try using get_class() instead.

    <?php 
    
    class A
    {
        public function test1()
        {
            echo __CLASS__ . "
    ";
        }
    
        public function test2()
        {
            echo get_class($this) . "
    ";
        }
    }
    
    class B extends A {}
    
    $b = new B;
    
    $b->test1();
    $b->test2();
    

    Output:

    A

    B

    Update

    In your particular situation, there are only two awesome methods; one defined on generation2 and one defined on generation1. generation3 and generation4 rely on their ancestor's definitions, and thus are not actually called inside of their context (this is why you only see two outputs, there's only two methods called).

    You could get around this by defining the awesome method at every level like you have it written. The trouble then is the $this->whoami() line. $this in each of those contexts is referring to a concrete instance of a generation4 object, so even at the generation2 level calling $this->whoami() inside of the awesome method will cause the method on generation4 to be called from all of the awesome methods.

    You can get around this limitation, too, by changing $this to be self. The final solution which I was able to come up with is this:

    class gen1
    {
        public function whoami()
        {
            echo __CLASS__ . PHP_EOL;
        }
        public function awesome()
        {
            self::whoami();
        }
    }
    
    class gen2 extends gen1 {
        public function whoami()
        {
            echo __CLASS__ . PHP_EOL;
        }
        public function awesome()
        {
            self::whoami();
            parent::awesome();
        }
    }
    class gen3 extends gen2 {
        public function whoami()
        {
            echo __CLASS__ . PHP_EOL;
        }
        public function awesome()
        {
            self::whoami();
            parent::awesome();
        }
    }
    class gen4 extends gen3 {
        public function whoami()
        {
            echo __CLASS__ . PHP_EOL;
        }
        public function awesome()
        {
            self::whoami();
            parent::awesome();
        }
    }
    
    $gen4 = new gen4;
    $gen4->awesome();
    

    Which gives an output of:

    gen4

    gen3

    gen2

    gen1

    You may be able to get around some of these limitations if you have PHP 5.5 installed (I am only on 5.4 locally right now, can't test this), but see this answer for some other possibilities.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 找人不需要人工智能回答的gamit解算后做形变分析
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题
  • ¥15 ogg dd trandata 报错