DEV Community

Discussion on: PHP Typed Properties: Think Twice.

na2axl profile image
Nana Axel

If we read this article carefully, we can see that the first example does not really break the principle of encapsulation. The first example shows two non typed properties with getters and setters, but theses ones doesn't provide any validation on the value defined by the user, they only provide type checking. The same type checking provided with public typed properties. Or the principle of encapsulation is to hide class implementation and control the state of the object, since the user can do anything stupid. When I see this it's just like C# auto-properties (a property with an empty getter and setter), it's a language concept.

Now for my opinion, this feature will make more senses if we can use embedded accessors like in the last example, so, interfaces declare properties like:

interface ProductInterface
    public int $price { get; }
    public string $description { get; set; }

So it will be the property itself which will provide encapsulation (again, like C# does).

But for now it remains a dream, but it will surely be a great feature, and have a ton of use cases, like for my ORM for example, which will allow the user to give an explicit type for properties mapped to database table columns.