What's the equivalent of virtual functions of c++ in PHP?

后端 未结 2 1636
生来不讨喜
生来不讨喜 2021-02-15 00:34

Is it abstract function xxx?

I just made a test which seems to indicate a private method to be virtual too?

class a {
 private function test         


        
2条回答
  •  再見小時候
    2021-02-15 01:22

    The example did not show a typical specialization pattern where b does not need to know implementation details of call() but can specify how test() is to be done. And it does return 1 unfortunately. However by declaring the function protected instead of private, it will work as expected.

    class a {
        protected function test()
        {
            echo 1;
        }
        public function call() {
            $this->test();
        }
    }
    
    class b extends a {
        protected function test()
        {
          echo 2;
        }
    }
    
    $instance = new b();
    $instance->call();
    

提交回复
热议问题