Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why do have some interfaces public properties? #131

Closed
k00ni opened this issue Nov 24, 2018 · 3 comments
Closed

Why do have some interfaces public properties? #131

k00ni opened this issue Nov 24, 2018 · 3 comments
Labels

Comments

@k00ni
Copy link
Contributor

k00ni commented Nov 24, 2018

(related to #130)

The specification describes interfaces as if they are (abstract) classes. In my understanding, interfaces should act as a contract, only defining methods. Therefore internal information should be hidden, which includes properties. But properties could be accessed using getter- and setter methods.

Based on the specification, i assume interfaces have only public properties? Which implies they are changeable by everybody.

The reason why i am asking is, because PHP interfaces can't have properties (fields). Its not your problem, but it would be cool, if we find some middle ground to reach 100% compatibility.

Can you give me a simple hint/link why this formalization was chosen? Thanks in advance.

@RubenVerborgh
Copy link
Member

RubenVerborgh commented Nov 24, 2018

Some interfaces have public properties, because it does not validate object encapsulation in JavaScript. They syntactically look like private field access, but can actually have setters and getters that guarantee encapsulation.

Therefore internal information should be hidden

Agreed.

which includes properties.

Disagreed. You likely mean "internal fields". Then I agree.

Which implies they are changeable by everybody.

Not in JavaScript.

But properties could be accessed using getter- and setter methods.

This statement is programming-language dependent.

JavaScript has properties that look like fields syntactically, but are actually getters and setters.

e.g., if you do something like triple.subject, then you are not necessarily accessing a private field. subject might very well be implemented with a getter and a setter.

The reason why i am asking is, because PHP interfaces can't have properties (fields).

And in Java, one would typically use .setSubject() and .getSubject() methods, because using .subject would mean exposing an internal field.

So when you see .subject in JavaScript, don't think "internal field, this is bad", but think "this is just syntax to access a getter or setter, it's safe".

Compare the Java way (e.g., https://stackoverflow.com/a/2963249/476820) to JavaScript getters and setters (e.g., https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get).

@RubenVerborgh
Copy link
Member

RubenVerborgh commented Nov 24, 2018

Example:

class Secret {
  get secret() {
    throw new Error('You cannot read the secret');
  }
  set secret(value) {
    throw new Error('You cannot change the secret');
  }
}

@k00ni
Copy link
Contributor Author

k00ni commented Nov 24, 2018

Alright, thanks for the quick clarification!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants