Welcome! Log In Create A New Profile

Advanced

[PHP] Operator for functions

Posted by Sam Hobbs 
Sam Hobbs
[PHP] Operator for functions
December 19, 2016 09:30PM
What is the "->" operator for functions called?

At the bottom of PHP: Operators - Manual
http://php.net/manual/en/language.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)."

It has been down-voted 81 times. Is that because the operator is not
called the "object operator"? Or is it because the note says that the
operator is not listed?

I apologize if my criticism is not appropriate, but the operator is not
properly documented. The "->" operator should be listed among the other
PHP operators and there should be documentation of it.

The C++ standard calls the "." and "->" operators the "dot" and "arrow"
operators, correspondingly. For example, see "5.2.4 Pseudo destructor
call" in:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
That is the latest draft of the newest C++ standard.

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. A book I am reading calls the operator
the "arrow operator". Is it called the object operator (as implied by
the tag) or the arrow operator or something else?
David Harkness
Re: [PHP] Operator for functions
December 19, 2016 10:00PM
Another fine distraction for my brain this morning! :)

On Mon, Dec 19, 2016 at 12:22 PM, Sam Hobbs <Sam@simplesamples.info> 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

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

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

Cheers!
David

[1]: https://news.ycombinator.com/item?id=2592040
Aziz Saleh
Re: [PHP] Operator for functions
December 19, 2016 10:20PM
On Mon, Dec 19, 2016 at 3:22 PM, Sam Hobbs <Sam@simplesamples.info> wrote:

> What is the "->" operator for functions called?
>
> 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)."
>
> It has been down-voted 81 times. Is that because the operator is not
> called the "object operator"? Or is it because the note says that the
> operator is not listed?
>
> I apologize if my criticism is not appropriate, but the operator is not
> properly documented. The "->" operator should be listed among the other PHP
> operators and there should be documentation of it.
>
> The C++ standard calls the "." and "->" operators the "dot" and "arrow"
> operators, correspondingly. For example, see "5.2.4 Pseudo destructor call"
> in:
> http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf
> That is the latest draft of the newest C++ standard.
>
> 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. A book I am reading calls the operator
> the "arrow operator". Is it called the object operator (as implied by the
> tag) or the arrow operator or something else?
>

There is a difference between a token and an operator, I think people
usually confuse the two. The -> is a parser token, indeed called the
T_OBJECT_OPERATOR (not sure why that comment was downvoted).

Here is a complete list of PHP's parser tokens:

http://php.net/manual/en/tokens.php
Christoph M. Becker
Re: [PHP] Operator for functions
December 20, 2016 01:40PM
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@simplesamples.info> 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
Sorry, only registered users may post in this forum.

Click here to login