encapsulation

Why would you declare getters and setters method private? [duplicate]

有些话、适合烂在心里 提交于 2019-11-27 02:33:44
问题 This question already has an answer here: Why use getters and setters/accessors? 38 answers I saw a code where getters and setters methods are declared private. I am trying to figure out the logic behind it, and I am really having hard time to understand why would you declare them as private? That's exactly opposite of what we are trying to achieve through getters and setters. 回答1: I can think of several reasons: you want to prevent future public access. If a different programmer sees your

Understanding the difference between __getattr__ and __getattribute__

巧了我就是萌 提交于 2019-11-27 02:29:13
I am trying to understand the difference between __getattr__ and __getattribute__ , however, I am failing at it. The answer to the Stack Overflow question Difference between __getattr__ vs __getattribute__ says: __getattribute__ is invoked before looking at the actual attributes on the object, and so can be tricky to implement correctly. You can end up in infinite recursions very easily. I have absolutely no idea what that means. Then it goes on to say: You almost certainly want __getattr__ . Why? I read that if __getattribute__ fails, __getattr__ is called. So why are there two different

Private vs. Public members in practice (how important is encapsulation?) [closed]

爷,独闯天下 提交于 2019-11-27 01:21:08
One of the biggest advantages of object-oriented programming is encapsulation, and one of the "truths" we've (or, at least, I've) been taught is that members should always be made private and made available via accessor and mutator methods, thus ensuring the ability to verify and validate the changes. I'm curious, though, how important this really is in practice. In particular, if you've got a more complicated member (such as a collection), it can be very tempting to just make it public rather than make a bunch of methods to get the collection's keys, add/remove items from the collection, etc.

I can't create a clear picture of implementing OOP concepts, though I understand most of the OOP concepts. Why? [closed]

醉酒当歌 提交于 2019-11-27 00:57:47
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 8 years ago . I have been working on some of the projects of my own and dont have any indrustial exposure. Currently i use simple approach for developing small applications with negligible OO approach like creating a common class for database functions using polymorphism of functions little bit use of constructors but i dont

What is encapsulation with simple example in php?

你说的曾经没有我的故事 提交于 2019-11-27 00:35:12
问题 What is encapsulation with simple example in php? 回答1: Encapsulation is just wrapping some data in an object. The term "encapsulation" is often used interchangeably with "information hiding". Wikipedia has a pretty thorough article. Here's an example from the first link in a Google search for 'php encapsulation': <?php class App { private static $_user; public function User( ) { if( $this->_user == null ) { $this->_user = new User(); } return $this->_user; } } class User { private $_name;

What is wrong with making a unit test a friend of the class it is testing? [duplicate]

╄→尐↘猪︶ㄣ 提交于 2019-11-27 00:16:29
问题 This question already has answers here : How do I test a private function or a class that has private methods, fields or inner classes? (51 answers) Closed last year . In C++, I have often made a unit test class a friend of the class I am testing. I do this because I sometimes feel the need to write a unit test for a private method, or maybe I want access to some private member so I can more easily setup the state of the object so I can test it. To me this helps preserve encapsulation and

Why “private” methods in the object oriented?

与世无争的帅哥 提交于 2019-11-27 00:09:11
I understand it is a very basic concept in the oops. But still I cannot get my head around. I understood why member variables are private, so class user cannot abuse it by setting up invalid values. But how can this apply to the methods ? trzewiczek Lot of good answers, but maybe one more from a self-taught Java programmer as I went through all that by myself with a lot of pain ;) Think about a Class as something seen from the outside, not as something you see internally . If you look at a Class from the outside, what you see? Taking the clock as an example again, a clock can give you info

Why use getters and setters [duplicate]

非 Y 不嫁゛ 提交于 2019-11-26 23:30:46
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why use getters and setters? Why use getters and setters? 回答1: To limit access to an internal state of an object. Consider the classic example of a class of geometric shapes. Say, a Circle object. When you set the radius, you may want to automatically calculate the area (which would be a read-only property) class Circle { float radius; public float Radius { get { return radius; } set { radius = value; area =

Property and Encapsulation

邮差的信 提交于 2019-11-26 23:24:20
问题 Following is a question regarding using properties in class. I have been using public properties instead of exposing member variables publically. Majority advise that this approach helps encapsulation. However, I don’t understand the encapsulation advantage by making it a property. many people donot know the real reason for using properties. They just do it as part of coding standard. Can someone clearly explain how a property is better than public member variable and how it improves

Why is it considered bad practice to use “global” reference inside functions? [duplicate]

末鹿安然 提交于 2019-11-26 23:17:29
Possible Duplicate: Are global variables in PHP considered bad practice? If so, why? global in functions Edit: question answered in link above. No, "global" in php is not the same thing as global in other languages, and while it does not introduce any security problems it can make the code less comprehensible to others. OP: Project summary - I am writing a web CMS to get my feet wet with PHP / MySQL. In order to break up the code I have a concept of these basic tiers / modules: Data - MySQL Tables - PHP Variables Function - SQL - Get / Set / etc - Frontend - Showing pages - Backend - Manager