2017-07-02 66 views
-2

下面是一個簡化我的代碼:如何在父類中訪問相同的名稱方法?

class questions { 

    public function index($one = '', $two = '', $three = '') { 
     return 'sth'; 
    } 
} 


class tags extends questions { 

    public function index() { 
     return parentClass::index(); 
    } 

} 

但我的代碼拋出這個錯誤:

enter image description here

是否有人知道我可以修正這個錯誤?

expected result is printing: sth

+0

檢查在autoloader.php中的代碼... – Jocelyn

+2

您應該使用'parent :: index()'從'tags'類調用'questions :: index()'而不是'parentClass :: index()'。 – rickdenhaan

+0

你試過回答問題:: index(); –

回答

2

如果要擴展一個類並重寫一個方法,你必須確保重載方法具有相同的「原型」,即它必須以相同的順序相同數量的方法參數。這就是爲什麼你得到的第一個警告:

Warning: Declaration of tags::index() should be compatible with questions::index($query_where = '', $query_join = '', $called_from = NULL) in C:\xampp\htdocs\myweb\others\tags.php on line 3

第二,如果你想調用與父類同名的功能,你需要使用parent關鍵字:

class tags extends questions { 

    public function index ($query_where = '', $query_join = '', $called_from = NULL) { 
     return parent::index($query_where, $query_join, $called_from); 
    } 

} 
相關問題