-
Notifications
You must be signed in to change notification settings - Fork 12
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
Comments
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.
Agreed.
Disagreed. You likely mean "internal fields". Then I agree.
Not in JavaScript.
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
And in Java, one would typically use So when you see 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). |
Example: class Secret {
get secret() {
throw new Error('You cannot read the secret');
}
set secret(value) {
throw new Error('You cannot change the secret');
}
} |
Alright, thanks for the quick clarification! |
(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.
The text was updated successfully, but these errors were encountered: