PHP从子对象中的父类函数调用覆盖子类函数

时间:2016-12-15 02:49:59

标签: php oop override method-overriding function-overriding

我有以下课程结构

class A 
{
  function aa(){
   //do something in aa
   $this->ab();
  }

  function ab(){
   //do something in ab
  }
}

Class B extends A
  function ab(){
   //do something else in ab
  }
}

$b = new B();
$b->aa();

当我从B类的对象调用aa()时,如何覆盖方法ab,以确保从B类调用它?

1 个答案:

答案 0 :(得分:1)

B类 - > ab()调用;

<?php
    class A 
    {
      function aa(){
       //do something in aa
       $this->ab();
      }

      function ab(){
       //do something in ab
       echo "class A\n";
      }
    }

    Class B extends A { 
      function ab(){
       //do something else in ab
       echo "class B\n";
      }
    }

    $b = new B();
    $b->aa();