2017-03-23 80 views
0

考慮下面的類:重寫父類方法的更精確的類型提示?

class objectCollection implements Iterator { 

    private $objectArray = []; 
    private $position = 0; 


    public function __construct() { 
     $this->position = 0; 
    } 

    public function add($object){ 
    if(!in_array($object, $this->objectArray)) 
     $this->objectArray[] = $object; 
    return $this; 
    } 

    public function remove($object){ 
    if(($key = array_search($object, $this->objectArray())) !== false) 
     unset($this->objectArray[$key]); 
    return $this; 
    } 

    public function rewind() { 
     $this->position = 0; 
    } 

    public function current() { 
     return $this->objectArray[$this->position]; 
    } 

    public function key() { 
     return $this->position; 
    } 

    public function next() { 
     ++$this->position; 
    } 

    public function valid() { 
     return isset($this->objectArray[$this->position]); 
    } 

} 

class attachmentCollection extends objectCollection { 

    public function add(attachment $attachment){ 
    return parent::add($attachment); 
    } 

    public function remove(attachment $attachment){ 
    return parent::remove($attachment); 
    } 

} 

這將產生follwing錯誤: attachmentCollection聲明:: Add()方法應與objectCollection ::添加($對象)

當你看兼容代碼,我認爲這是我想要做的很明顯。 我希望attachmentCollection基本上與objectCollection相同,除了可以添加(或刪除)的對象需要是attachment的實例。

這樣做的正確方法是什麼?

+0

儘可能我知道,PHP不支持與高級語言完成相同的方式重載...這些必須具有相同的簽名... –

+0

哦,好的。這是一個無賴。 – minychillo

回答

0

PHP errored code

PHP working code

更改爲

public function add($object){ 
    if(!in_array($object, $this->objectArray)) 
     $this->objectArray[] = $object; 
    return $this; 
    } 


public function remove($object){ 
    if(($key = array_search($object, $this->objectArray())) !== false) 
     unset($this->objectArray[$key]); 
    return $this; 
    } 

public function add(attachment $object){ 
    if(!in_array($object, $this->objectArray)) 
     $this->objectArray[] = $object; 
    return $this; 
} 
public function remove(attachment $object){ 
    if(($key = array_search($object, $this->objectArray())) !== false) 
     unset($this->objectArray[$key]); 
    return $this; 
    } 
+0

謝謝你的回答!當然,這樣可以消除這個錯誤,但我不能從具有不同類型提示的objectCollection中擴展其他類。 與'productCollection'一樣,例如'add'方法只接受'產品'對象 – minychillo

+0

@minychillo如果您使用的是'strict_types',那麼您也必須在父類中關注它。 –

0

如果沒有人有一個更好的解決辦法,這裏是我去了(還開出了更好的解決方案):

class attachmentCollection extends objectCollection { 

    public function add($attachment){ 
    if(! $attachment instanceof attachment) throw new \Exception('Argument must be instance of attachment'); 
    return parent::add($attachment); 
    } 

    public function remove($attachment){ 
    if(! $attachment instanceof attachment) throw new \Exception('Argument must be instance of attachment'); 
    return parent::remove($attachment); 
    } 

} 
0
的情況下

你想從objectClass的進一步延伸的類和方法應該是兼容的,那麼我會建議使用一個接口作爲傳遞的參數,例如:

interface collection{  
    //your methods 
} 

class objectCollection implements Iterator { 

    private $objectArray = []; 
    private $position = 0; 

    public function add(collection $object){ 
     if(!in_array($object, $this->objectArray))      
     $this->objectArray[] = $object; 
     return $this; 
    } 

    public function remove(collection $object){ 
     if(($key = array_search($object, $this->objectArray())) !==false) 
     unset($this->objectArray[$key]); 
     return $this; 
    } 
    .... 
} 


class attachmentCollection extends objectCollection { 

    public function add(collection $attachment){ 
     return parent::add($attachment); 
    } 

    public function remove(collection $attachment){ 
     return parent::remove($attachment); 
    } 

} 

class productCollection extends objectCollection { 

    public function add(collection $attachment){ 
     return parent::add($attachment); 
    } 

    public function remove(collection $attachment){ 
     return parent::remove($attachment); 
    } 

} 


class attachment implements collection { 
    //implement interface methods 
} 

class product implements collection{ 
    //implement interface methods 
} 
相關問題