Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 接口对象在函数参数中的含义是什么_Php_Laravel_Oop_Yii - Fatal编程技术网

Php 接口对象在函数参数中的含义是什么

Php 接口对象在函数参数中的含义是什么,php,laravel,oop,yii,Php,Laravel,Oop,Yii,我无法理解将任何模型或接口对象放入方法参数中的含义。 比如说, public function checkRights(CommentInterface $comment) { return true; } 那么,CommentInterface在这里做什么呢?为什么我们不仅仅在这里传递$comment?在编程语言中如何命名这种东西 我不熟悉面向对象的php 谢谢。这称为类型提示 类型暗示强制您仅传递特定类型的对象。这可以防止您传递不兼容的值,如果您正在与团队合作,则会创建一个标准 检

我无法理解将任何模型或接口对象放入方法参数中的含义。 比如说,

public function checkRights(CommentInterface $comment)
{
    return true;
}
那么,CommentInterface在这里做什么呢?为什么我们不仅仅在这里传递$comment?在编程语言中如何命名这种东西

我不熟悉面向对象的php
谢谢。

这称为类型提示

类型暗示强制您仅传递特定类型的对象。这可以防止您传递不兼容的值,如果您正在与团队合作,则会创建一个标准

检查以下示例:

class Profile {
private $setting;
public function __construct(Setting $setting)
    {
        $this->setting = $setting;
    }
}

因为我们需要在函数中使用$setting对象,所以我们将/pass/type hint作为参数注入它。

这是一个类型提示。这意味着
$comment
必须是实现
CommentInterface
接口的对象。看,不,那不是DI
CommentInterface
是一个简单的类型提示,并非所有类型提示都引用DI