覆盖静态方法时PHP不执行任何操作

时间:2017-06-27 04:20:20

标签: php

考虑一下:

class Player {

    public static function echoSomthing(){
        echo "A";
    }

}

class ModifiedPlayer extends Player {

        public static function echoSomthing(){
            echo "B";
        } 
}

好的,所以我覆盖了我的静态echoSomthing函数,但“A”仍然得到回应。我做错了什么?

2 个答案:

答案 0 :(得分:0)

正如Sahil GulatipAsh以及其他

的评论中所述

我们需要使用范围分辨率运算符::

访问任何静态方法

请运行以下代码,然后输出B

<?php
class Player {
    public static function echoSomthing() {
        echo "A";
    }
}
class ModifiedPlayer extends Player {
    public static function echoSomthing() {
        echo "B";
    }
}
ModifiedPlayer::echoSomthing();

Demo

答案 1 :(得分:0)

class Player {

    public static function echoSomthing(){
        echo "A";
    }

}

class ModifiedPlayer extends Player {

    public static function echoSomthing(){
        echo "B";
    }
}    

$ob = new ModifiedPlayer();    
$ob->echoSomthing(); // B displayed here