Web lists-archives.com

Re: [PHP] Operator for functions




On 19.12.2016 at 21:54, David Harkness wrote:

> 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"
>> (T_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

According to the PHP language specification[1] (which is deliberately
much more precise than the PHP manual), "member access operator" might
be more appropriate.

>> 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;
>     var_dump($f);
> 
>     > object(Bar)#1 (1) {
>     > ["baz"]=>
>     >   string(6) "Foobar"
>     > }

It's hardly possible to give exact precendence rules for all operator,
because the expression syntax isn't that uniform.  This has been
improved in PHP 7, but there are still several special cases.  For
instance, new_expr is not just `new expr`[2].

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

Mailing to the doc list would be appropriate, but filing a doc bug
(<https://bugs.php.net/>) appears to be most appropriate.

>> 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".
> 
> Cheers!
> David
> 
>   [1]: https://news.ycombinator.com/item?id=2592040

[1]
<https://github.com/php/php-langspec/blob/master/spec/10-expressions.md#member-access-operator>
[2] <http://lxr.php.net/xref/PHP-7.1/Zend/zend_language_parser.y#860>

-- 
Christoph M. Becker

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php