php/php-langspec

PHP7+ and(), or() methods

bpolaszek opened this issue · 2 comments

Hello there,

I'm not sure wether or not this is the good place to ask*, but I've figured out this:

class A {

    private $value = 'foo';

    public function and($value) {
        return $this->value . ',' . $value;
    }

}

$a = new A();
print($a->and('bar'));

PHP7+ result:

foo,bar

PHP previous versions (tested on 5.6):

PHP Parse error: syntax error, unexpected 'and' (T_LOGICAL_AND), expecting identifier (T_STRING)

The ability to use control structures like and/or operators as object methods can be very helpful in creating intuitive fluent interfaces, but this new behaviour in PHP7 is not documented anywhere.

So I was wondering if this is something normal and desired for PHP7+, (then we can safely rely on this to publish PHP projects / libraries), or is this something completely unwanted that may be removed in a further version?

Thank you much,
Ben

* If I'm in the wrong place, please tell me where can I post that topic!

This is the intended behavior, and was implemented as part of this RFC: https://wiki.php.net/rfc/context_sensitive_lexer

Hi @mattacosta,

Brilliant! These changes were not reflected in PHP 7.0 new features and it was pretty difficult to google terms like and or or to find the correct documentation 😝

Thank you, that's great news! 🎉

Ben