encapsulation

Why doesn't PHP permit private const?

…衆ロ難τιáo~ 提交于 2019-11-27 20:20:46
I have a class that benefits from the use of constants in its internal implementation, but I would like to limit visibility of these constants. Why doesn't PHP permit private constants? Is there another way to achieve this or is PHP trying to discourage some type of design misstep I am ignorant of? sudhir chauhan Use private static properties. In that case you will have the same variable throughout all objects and if you want to extend its scope to nested, you can expose a getter method to get its value and restrict variables settings. As of PHP 7.1, there are real private constants. private

What is the use of encapsulation when I'm able to change the property values with setter methods?

心已入冬 提交于 2019-11-27 19:49:55
I try to understand a lot of times but I failed to understand this. Encapsulation is the technique of making the fields in a class private and providing access to the fields via public methods. If a field is declared private, it cannot be accessed by anyone outside the class, thereby hiding the fields within the class. How can we change the values of fields through setter methods? How do we prevent accessing the fields directly? What is the real use of encapsulation? Assume you have an age property. The user can enter a value of -10 , which although is a valid number, is an invalid age. A

Encapsulation vs Data Hiding - Java

跟風遠走 提交于 2019-11-27 17:17:34
Interviewer: What is encapsulation and how do you achieve it in Java? Me: Encapsulation is a mechanism to hide information from the client. The information may be data or implementation or algorithm. We achieve this using access modifiers. Interviewer: This is data hiding. How do we achieve encapsulation in Java? Me : uummmm Concrete Question: Other than 'Access Modifiers', what is the way to implement Encapsulation in Java? More generally encapsulation refers simply to bundling the data (e.g. of an object) with the operations on that data. So you have a class encapsulating data - fields -

Why can private member variable be changed by class instance?

白昼怎懂夜的黑 提交于 2019-11-27 17:12:57
问题 class TestClass { private string _privateString = "hello"; void ChangeData() { TestClass otherTestClass = new TestClass(); otherTestClass._privateString = "world"; } } This code compiles in C# and the equivalent works in PHP, but can someone explain the reason why otherTestClass._privateString can be changed here ? I would have thought an instance of a class should not be able to change a private member variable under any circumstances, and that trying to access otherTestClass._privateString

Must Dependency Injection come at the expense of Encapsulation?

给你一囗甜甜゛ 提交于 2019-11-27 16:44:13
If I understand correctly, the typical mechanism for Dependency Injection is to inject either through a class' constructor or through a public property (member) of the class. This exposes the dependency being injected and violates the OOP principle of encapsulation. Am I correct in identifying this tradeoff? How do you deal with this issue? Please also see my answer to my own question below. There is another way of looking at this issue that you might find interesting. When we use IoC/dependency injection, we're not using OOP concepts. Admittedly we're using an OO language as the 'host', but

Check if class is derived from a specific class (compile, runtime both answers available)

我的未来我决定 提交于 2019-11-27 14:49:16
It is easier to explain on an example so, class base { //.... } class derived1 : public base { //... } In my library, there is a pointer of base class. The user of the library have to make classes derived from either base or derived1 and assign pointer to that class. How can I check what class is user-defined class derived from? Cassio Neri I have some remarks on the proposed compile-time x runtime solutions. In addition to when they are evaluated, is_base_of and dynamic_cast have different requirements and their answers can be different. (1) First of all (as pointed out by others) to use

Can you explain this thing about encapsulation?

混江龙づ霸主 提交于 2019-11-27 13:58:08
问题 In response to What is your longest-held programming assumption that turned out to be incorrect? question, one of the wrong assumptions was: That private member variables were private to the instance and not the class. (Link) I couldn't catch what he's talking about, can anyone explain what is the wrong/right about that with an example? 回答1: public class Example { private int a; public int getOtherA(Example other) { return other.a; } } Like this. As you can see private doesn't protect the

SessionsHelper in railstutorial.org: Should helpers be general-purpose modules for code not needed in views?

淺唱寂寞╮ 提交于 2019-11-27 13:44:18
问题 railstutorial.org has a suggestion which strikes me as a little odd. It suggests this code: class ApplicationController < ActionController::Base protect_from_forgery include SessionsHelper end The include SessionsHelper makes the methods available from ApplicationController , yes, but it makes them available in any view, as well. I understand that authentication/authorization is cross-cutting, but is this really the best place? That seems to me to be potentially too broad of a scope. Putting

Java encapsulation [duplicate]

自古美人都是妖i 提交于 2019-11-27 11:08:49
This question already has an answer here: Why use getters and setters/accessors? [closed] 38 answers We always say that data will be encapsulated if we simply define variables private and define getters setters to access those variables. My question is if we can access the variables (data) though via getters and setters, how come data is hidden or safe? I googled a lot for an explanation, but I found nothing. Every one just said in their blogs and posts that it is a data hiding technique, but it has not been explained/elaborated. mithilatw The way I understand your question is, although we

Encapsulation vs Information hiding

眉间皱痕 提交于 2019-11-27 10:34:47
问题 What exactly are the differences between Ecapsulation and Information Hiding? Well i know that making fields private and then making setter and getter of the fields is ecapsulation.However does encapsulation mean just this? suppose i have a class as described below. public Class IsThisEncapsulation { public int age; public void setAge(int age) { this.age=age; } public int getAge() { return age; } } Now is the class IsThisEncapsulation an example of Encapsulation? Now would making the field