2013-10-24 75 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) { 
}