2016-12-16 137 views
2

我有這個特定的情況,我的特徵有一個方法,我的類有一個方法,都具有相同的名稱。具有相同名稱的Php類和特徵方法

我需要使用這兩種方法(所述一個從所述性狀和類)內部那類,其中包含相同的方法

namespace Some\Namespace; 
use Some\Other\Namespace\TestTrait; 

class TestClass { 

    use TestTrait; 

    public function index() 
    { 
    // should call the method from the class $this->getId(); 
    // should also call the method from the trait $this->getId(); 
    } 

    private function getId() 
    { 
    // some code 
    } 
} 

而在單獨定義性狀:

trait TestTrait 
{ 
    private function getId() 
    { 
     // does something 
    } 
} 

請注意這不是粘貼代碼,我可能會有一些錯別字:P

回答

3

使用特性Conflict Resolution

namespace Some\Namespace; 
use Some\Other\Namespace\TestTrait; 

class TestClass { 

    use TestTrait { 
     getId as traitGetId; 
    } 

    public function index() 
    { 
    $this->getId(); 
    $this->traitGetId(); 
    } 

    private function getId() 
    { 
    // some code 
    } 
} 
+0

感謝您的回答,(對不起,對於遲到的回覆)我想這會工作,介意我仍然想在我的代碼中測試這個,只是爲了100%確定:) – 0CDc0d3r

相關問題