覆盖扩展类的方法可见性

时间:2016-12-09 19:41:27

标签: php oop

有没有办法在不覆盖整个方法的情况下覆盖扩展类方法的可见性?

class A
{
    public function perform()
    {
        // Do a bunch of stuff that you don't want to override.
    }
}

class B extends A
{

    /*
    * Change perform()'s viability from public
    * to protected without re-coding the whole method.
    */
}

// You can not do this.
$b = new B();
$b->perform();

1 个答案:

答案 0 :(得分:1)

您可以创建一个简单调用父方法的受保护方法:

class B extends A
{
    protected function perform() {
        parent::perform();
    }
}