方法中的类名以及变量 [英] class name in method along with variable

查看:92
本文介绍了方法中的类名以及变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读有关PHP中的设计模式的信息,并且不断看到以下语法,例如

I am reading about design patterns in PHP and I keep seeing the following syntax, for example

abstract class AbstractObserver {
    abstract function update(AbstractSubject $subject_in);
}

class PatternObserver extends AbstractObserver {
    public function __construct() {
    }
    public function update(AbstractSubject $subject) {   
    }
}

(代码是示例)

其中 AbstractSubject是另一个抽象类。

where "AbstractSubject" is another abstract class.

我习惯于定义诸如 methodName($ var)之类的方法,其中不包括类名,例如 methodName(className $ var)

I am used to defining methods like methodName($var), not including a class name in there, like methodName(className $var).

那么,类名实际上在方法中做什么?我最好的猜测是它在该类中传递了类似参考的内容?你能告诉我它的实际作用吗?

So, what actually the class name does in a method ? My best guess is that it passes something like a reference in that class? Can you explain to me what it actually does?

谢谢

推荐答案

它称为类型提示,因为php 7可以使用标量类型以键入提示参数。 (更多

It's called Type Hinting, since php 7 you can use scalar types to type hint parameters. (Some more)

这篇关于方法中的类名以及变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆