Encapsulate behaviour with anonymous classes

Today, a colleague – Joel Piccoli da Rosa – sent a link to a blog post by a fellow developer, Sebastian De Deyne, where Sebastian describes something he wished PHP had native support: nested properties. Something like the pseudo-code the following image shows:

I found the article very interesting and I understand why Sebastian would like something like what exists in Typescript. It’s a very brief and concise syntax. He also says that not having this leads devs to either, and I’m quoting, “[…] use less types (by using an associative array) or worse: create the wrong abstraction (abstract class Endpoints is not a good use of inheritance).”. +1 for not recommending inheritance!

The gist of the problem, as I understood, was how to encapsulate the “endpoints” information without using associative arrays or external classes (abstract or not) that would only be used within the context of “ContactResource” without losing type inference, autocompletion, and all those goodies. This got me thinking… And I reached this solution:

Is it as elegant as Sebastian’s proposed syntax? Nope. But I don’t think it’s ugly or unmanageable, either. I find it quite elegant: we have encapsulation, we don’t use associative arrays, and we don’t point to external classes that will only be used in this context.