Use derived class functions in php base class

PHP can use derived class functions in the base class, but the base class object cannot use this function that uses derived class functions:

class a {
    
    
    public function func1() {
    
    
        $this->func2();    // 虽然没有func2的定义,但也能通过编译
    }
}

class b extends a {
    
    
    public function func2() {
    
    
        print("in b->func2");
    }
}

$bObj = new b();
$bObj->func1();    // 正确

$aObj = new a();
$aObj->func1();    // 报错

Run it:
Insert picture description here

Guess you like

Origin blog.csdn.net/tus00000/article/details/111885627