繁体   English   中英

请解释此函数中的参数

[英]pls explain the argument in this function

请看下面的代码函数isInMatchingSet(卡$卡)正在拍“卡$卡”通常我看到“$ 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());
    }
}

这称为类型提示 它是在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) {
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM