Hey Rob,

On 14.3.2025 00:26:03, Rob Landers wrote:
My biggest issue with `::` is that it gets weird:

class Foo {
  public class Bar {}
  public const Bar = "";
  public static function Bar() {}
}

echo Foo::Bar; // this is the constant
new Foo::Bar(); // this is the class
Foo::Bar(); // this is the method
new (Foo::Bar()); // this is the method
new (Foo::Bar); // this is constant

I can now differentiate between these all in the AST, but it seems weird to me. If we go this route, I'd personally have the preference to allow them all and let people's code-style dictate what is acceptable or not -- assuming I can ensure there is no ambiguity in the grammar. At least with `:>` (or something else) we don't have to even have that discussion. :)

Why would that be weird?

In 99% of the cases new followed by something followed by double colons followed by something else is just the inner class.

Writing new (Foo::Bar) or new (Foo::Bar()) already today looks suspicious and will remain looking suspicious. Nothing will change about that. And "Foo::Bar" (or "Foo::Bar()") without being preceded by "new" or followed by "::" is just the normal class constant.

On top of that, there are naming conventions in PHP which will make it even more obvious: class constants in uppercase and methods in camelCase and class names in PascalCase. So, just looking at Foo::Bar (without considering any surrounding tokens), you expect a class name. Looking at Foo::BAR, you expect a constant. Looking at Foo::bar you expect a method.


echo Foo::BAR; // this is the constant
new Foo::Bar(); // this is the class
Foo::bar(); // this is the method

new (Foo::bar()); // this is the method
new (Foo::BAR); // this is constant
// or alternatively:
$myFantasticInterface = Foo::bar();
new $myFantasticInterface;
$myAwesomeInterface = Foo::BAR;
new $myAwesomeInterface;

There's no real surprises at a glance as long as you don't intentionally make your code obscure.


Bob

Reply via email to