方法中的类名以及变量 [英] class name in method along with variable
问题描述
我正在阅读有关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屋!