Web lists-archives.com

Re: [PHP] Operator for functions

Another fine distraction for my brain this morning! :)

On Mon, Dec 19, 2016 at 12:22 PM, Sam Hobbs <Sam@xxxxxxxxxxxxxxxxxx> wrote:

> What is the "->" operator for functions called?

First, I would use "methods" instead of "functions" here because it doesn't
apply to functions per se. Rather, it applies to object instances to
provide access to their properties and methods (yes, those are called
functions, too). Normally I wouldn't bring it up, but it could be confusing
when discussing the details.

At the bottom of PHP: Operators - Manual <http://php.net/manual/en/lang
> uage.operators.php> (http://php.net/manual/en/language.operators.php) is
> the note:
> "The -> operator, not listed above, is called "object operator"

I'd probably go with "object access operator," but the above is fine and
matches the token name. Of course, you won't hear me trying to pronounce
the token name for the "::" operator (T_PAAMAYIM_NEKUDOTAYIM [1]) any time
soon. :p

It has been down-voted 81 times.

That is just silly! Equally silly is the -25 score for said "::" operator.

    "The scope resolution operator ::, which is missing from the list
above, has higher precedence than [],
    and lower precedence than 'new'. This means that self::$array[$var]
works as expected."

I'm not sure about the precedence compared to the "new" operator, though.
This test tells me that "::" has higher precedence than "new":

    class Foo {
        static $bar = 'Bar';
    class Bar {
        var $baz = 'Foobar';
    $f = new Foo::$bar;

    > object(Bar)#1 (1) {
    > ["baz"]=>
    >   string(6) "Foobar"
    > }

Clearly, the interpreter used "Foo::$bar" to determine the name of the
class to instantiate with "new". But I digress...

PHP can call its "->" operator whatever the designers decide, but there
> should be a definition to eliminate the confusion of different names
> depending on personal preference.

Agreed. You could send an email to the developers' mailing list to have
them add it to the documentation.

A book I am reading calls the operator the "arrow operator".

I'd hesitate to call it the "arrow operator" to avoid confusion with "=>".
I'd go with "object operator".


  [1]: https://news.ycombinator.com/item?id=2592040