2013-10-24 74 views
1

請看下面的代碼函數isInMatchingSet(Card $ card) 正在服用「card $ card」通常我看過「$ one,$ two」 當我編碼時永遠不會有像「卡「 那麼這裏的卡是什麼?我在猜測它的類名。 我找不到一個很好的解釋,所以我想在這裏問 。請說明這個函數的參數

<?php 

/** 
* Models an individual card. 
*/ 
class Card 
{ 
    /** 
    * @var string 
    */ 
    private $number; 

    /** 
    * @var string 
    */ 
    private $suit; 

    /** 
    * @param string $number 
    * @param string $suit 
    */ 
    public function __construct($number, $suit) 
    { 
     $this->number = $number; 
     $this->suit = $suit; 
    } 

    /** 
    * @return string 
    */ 
    public function getNumber() 
    { 
     return $this->number; 
    } 

    /** 
    * @return string 
    */ 
    public function getSuit() 
    { 
     return $this->suit; 
    } 

    /** 
    * Returns true if the given card is in the same set 
    * @param Card $card 
    * @return bool 
    * @assert (new Card(3, 'h'), new Card(3, 's')) == true 
    * @assert (new Card(4, 'h'), new Card(3, 's')) == false 
    */ 
    public function isInMatchingSet(Card $card) 
    { 
     return ($this->getNumber() == $card->getNumber()); 
    } 
} 
+0

指示參數是哪個類/應 –

+1

它是[類型提示]的(http://php.net/manual/en/language.oop5.typehinting.php )。 – naththedeveloper

+0

謝謝我這麼認爲,但並不太確定 –

回答

4

這叫做type hinting。它在PHP 5中引入。

PHP 5引入了類型提示。函數現在可以強制參數成爲對象(通過在函數原型中指定類的名稱),接口,數組(自PHP 5.1起)或可調用(自PHP 5.4起)。但是,如果將NULL用作默認參數值,則它將被允許作爲任何稍後調用的參數。

實例:

// Array — expects an array 
function test(Array $array) { 
} 

// Interface — expects an object of a class implementing the given interface 
function test(Countable $interface) { 
} 

// Class — expects an object of that class (or any sub-classes) 
function test(Exception $object) { 
} 

// Callable — expects any callable object 
function test(callable $callable) { 
}