What is the "->" PHP operator called?

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

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

How do you pronounce it when reading code out loud?

78996 次浏览

The official name is "object operator" - T_OBJECT_OPERATOR.

Property operator.

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

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()".

The senior PHP developer where I work says "arrow".

$A->B;

When he's telling me to type the above, he'll say, "Dollar A arrow B" or for

$A->B();

"Dollar A arrow B parens."

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.

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

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"

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

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

$a->b

I call as "param b of $a".

$a->b()

I call as "function b of $a".

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.

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.

I call it "dart"; as in $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! ;)

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

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".

Object. So $a->$b->$c would be 'A object B object c'.

I would do it like this:

//Instantiated object variable with the name mono call property name
$mono->name;


//Instantiated object variable with the name mono call method ship
$mono->ship();

In my book (PHP Master by Lorna Mitchell) it's called the object operator.

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

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."

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."

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.

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

user187291 has answered the non-subjective question, as for the subjective one, I say "sub". For example, I would pronounce $x->y as "x sub y" - "sub" in this context is short for "subscript". This is more appropriate for array notation; $x['y'] I also pronounce "x sub y". Typically when reading code out loud, I am using my words to identify a line the other developer can see, so the ambiguity has yet to become a problem. I believe the cause is that I view structs/objects and arrays as "collections", and elements thereof are subscripted as in mathematical notation.

Japanese has a convenient particle for this, "no" (の). It is the possessive particle, meaning roughly "relating to the preceding term".

"Fred の badger" means "Fred's badger".

Which is a long way round of saying that at least mentally, and also amongst those who understand Japanese, I tend to differentiate them by reading them as:

$A->B(); // "Call $A's B."
C::D();  // "Call C の D."

But reading both as a possessive "'s" works too, for non-Japanese speakers. You can also flip them and use "of", so "D of C"... but that's kinda awkward, and hard to do it you're reading rapidly because it breaks your linear flow.

If I were dictating, though, perhaps when pair coding and suggesting what to type, I'd refer to the specific characters as "dash-greater-than arrow" and "double-colon" (if i know the person I'm talking to is a PHPophile, I might call that a "paamayim nekudotayim double-colon" the first time, partly because it's a cool in-joke, partly to prevent them from "correcting" me).