PHP 运算符“->”是什么?大声朗读代码时怎么说?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2588149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:02:17  来源:igfitidea点击:

What is the "->" PHP operator called and how do you say it when reading code out loud?

phpoperators

提问by Jake

What do you call this arrow looking ->operator found in PHP?

你把这个->在 PHP 中找到的箭头操作符称为什么?

It's either a minus sign, dash or hyphen followed by a greater than sign (or right chevron).

它是一个减号、破折号或连字符,后跟一个大于号(或右 V 字形)。

How do you pronounce it when reading code out loud?

大声朗读代码时如何发音?

回答by user187291

The official name is "object operator" - T_OBJECT_OPERATOR. I call it "arrow".

正式名称是“对象运算符” - T_OBJECT_OPERATOR。我称之为“箭头”。

回答by Umbrella

I call it "dart"; as in $Foo->bar(): "Foo dart bar"

我称之为“飞镖”;如$Foo->bar():“Foo dart bar”

Since many languages use "dot" as in Foo.bar();I wanted a one-syllable word to use. "Arrow" is just too long-winded! ;)

由于许多语言使用“点”,因为Foo.bar();我想使用一个单音节词。“箭”实在是太啰嗦了!;)

Since PHP uses ."dot" for concatenation (why?) I can't safely say "dot" -- it could confuse.

由于 PHP 使用.“点”进行连接(为什么?)我不能安全地说“点”——它可能会混淆。

Discussing with a co-worker a while back, we decided on "dart" as a word similar enough to "dot" to flow comfortably, but distinct enough (at least when we say it) to not be mistaken for a concatenating "dot".

不久前与一位同事讨论,我们决定将“dart”作为一个与“dot”足够相似的词来舒适地流动,但足够清晰(至少当我们说它时)不会被误认为是连接的“点” .

回答by selfawaresoup

When reading PHP code aloud, I don't pronounce the "->" operator. For $db->prepare($query);I mostly say "Db [short pause]prepare query." So I guess I speak it like a comma in a regular sentence.

大声朗读 PHP 代码时,我不发音“->”运算符。因为$db->prepare($query);我主要说“Db [短暂停顿]准备查询”。所以我想我说它就像一个普通句子中的逗号。

The same goes for the Paamayim Nekudotayim ("::").

Paamayim Nekudotayim ("::") 也是如此。

回答by Dan Mantyla

When reading the code to myself, I think of it like a "possessive thing".

在给自己读代码时,我认为它是一种“占有欲”。

For example:

例如:

x->value = y->value

would read "x's value equals y's value"

会读“x 的值等于 y 的值”

回答by sprugman

Most often, I use some variation on @Tor Valamo's method ("the B method of A" or "A's B method"), but I sometimes say "dot". E.g. "call A dot B()".

大多数情况下,我使用@Tor Valamo 方法的一些变体(“A 的 B 方法”或“A 的 B 方法”),但有时我会说“点”。例如“调用 A 点 B()”。

回答by Tor Valamo

Property operator.

物业运营商。

When reading $a->b()or $a->bloud I just say "call b on the $a obj" or "get b from/in/of $a"

在阅读$a->b()$a->b大声朗读时,我只是说“在 $a obj 上调用 b”或“从 $a 中/在/中获取 b”

回答by Don

Harkening back to the Cobol 'in' where you would say "Move 5 to b in a." Most languages today qualify things the other direction.

回到 Cobol 'in' 你会说“Move 5 to b in a”。今天的大多数语言都在另一个方向上限定事物。

Yet I would still read $a->b();as "Call b in a".

但我仍然会读$a->b();为“在 a 中调用 b”。

回答by ganu

$a->b 

I call as "param b of $a".

我称之为“$a 的参数 b”。

$a->b()

I call as "function b of $a".

我称之为“$a 的函数 b”。

回答by Craige

I personally like to be verbose in expressing my code verbally.

我个人喜欢在口头表达我的代码时变得冗长。

e.g.:

例如:

$foo = new Foo();
echo $foo->bar

would read as such:

会这样读:

echo(/print) the bar property of object foo.

echo(/print) 对象 foo 的 bar 属性。

It's verbose and more time consuming, but I find if there is a reason for me to be expressing my code verbally, then I probably need to be clear as to what I'm communicating exactly.

它冗长且耗时,但我发现如果我有理由口头表达我的代码,那么我可能需要清楚我正在传达的内容。

回答by user253780

The single arrow can easily be referred verbally as what it means for PHP OOP: Member. So, for $a->varyou would say "Object a's member var".

单箭头很容易在口头上被称为它对 PHP OOP 的意义:成员。所以,因为$a->var你会说“对象 a 的成员变量”。

When reading code aloud, it does help to read ahead (lookahead reference, sorry), and know what you may actually be referring to. For instance, let's have the following bit of code:

大声阅读代码时,提前阅读确实有帮助(先行参考,抱歉),并知道您实际上可能指的是什么。例如,让我们有以下代码:

<?php
  Class MyClass {
    $foo = 0;

    public function bar() {
      return $this->foo;
    }
  }

  $myobject = new MyClass();
  $myobject->bar();
?>

So, if I were to read aloud this code block as a demonstration of code, I would say this:

所以,如果我要大声朗读这个代码块作为代码演示,我会这样说:

"Define Class 'MyClass', open-brace. Variable 'foo' equals zero, terminate line. Define public function 'bar' open-close parentheses, open-brace. Return member variable 'foo' of object 'this', terminate line. Close-brace, close-brace. Instantiate new instance of 'MyClass' (no arguments) as variable object 'myobject', terminate line. Call member method 'bar' of object 'myobject', terminate line."

"定义类'MyClass',开括号。变量'foo'等于0,终止行。定义公共函数'bar'开闭括号,开括号。返回对象'this'的成员变量'foo',终止行. Close-brace, close-brace. 将'MyClass'(无参数)的新实例实例化为变量对象'myobject',终止行。调用对象'myobject'的成员方法'bar',终止行。”

However, if I were reading the code aloud for other students or programmers to copy without a visual, then I would say:

但是,如果我大声朗读代码供其他学生或程序员在没有视觉效果的情况下复制,那么我会说:

"PHP open tag (full), newline, indent, Class MyClass open-brace, newline. Indent, ['dollar-sign' | 'Object-marker'] foo equals 0, semicolon, newline, newline. public function bar open-close parentheses, open-brace, newline. Indent, return ['dollar-sign' | 'Object-marker'] this arrow foo, semicolon, newline. Reverse-indent, close-brace, newline, reverse-indent, close-brace, newline, newline. ['dollar-sign' | 'Object-marker'] myobject equals new MyClass open-close parentheses, semicolon, newline. ['dollar-sign' | 'Object-marker'] myobject arrow bar open-close parentheses, semicolon, newline. Reverse-indent, PHP close tag."

"PHP open tag (full), newline, indent, Class MyClass open-brace, newline. Indent, ['dollar-sign' | 'Object-marker'] foo equals 0, 分号, newline, newline. public function bar open-关闭括号,左括号,换行符。缩进,返回 ['dollar-sign' | 'Object-marker'] 这个箭头 foo,分号,换行符。反向缩进,关闭大括号,换行符,反向缩进,关闭大括号, 换行符, 换行符。['dollar-sign' | 'Object-marker'] myobject 等于新的 MyClass 开闭括号、分号、换行符。['dollar-sign' | 'Object-marker'] myobject 箭头栏开闭括号、分号、换行符。反向缩进,PHP 结束标记。”

Where you see ['dollar-sign' | 'Object-marker'], just choose whichever you tend to speak for '$', I switch between the two frequently, just depends on my audience.

你在哪里看到 ['美元符号' | 'Object-marker'],就选择你倾向于代表'$'的那个,我经常在两者之间切换,这取决于我的听众。

So, to sum it up, in PHP, -> refers to a member of an object, be it either a variable, another object, or a method.

因此,总而言之,在 PHP 中,-> 指的是对象的成员,无论是变量、另一个对象还是方法。