getters

How do I use PHP's __get() and __set() for defined fields in my class?

I've had a good read of the PHP specs on overloading, and most of the examples appear to be intended to simply allow defining of custom fields (similar to stdClass). But what about the private fields defined in my class? How should these be retrieved/assigned? I could do a switch on possible values and act on certain values: class A { ...

Can I use mvc without getters and setters?

If I don't want to expose the state of my object, but I still need to display it (in HTML, XML or JSON let's say), how would I go about doing that in an MVC environment. Does it make sense to have an export method which exports a dumbed down immutable object (a "data class" if you will). What about adding in a render method which talks...

In Java is there a performance difference between referencing a field through getter versus through a variable?

Is there any differences between doing Field field = something.getSomethingElse().getField(); if (field == 0) { //do something } somelist.add(field); versus if (something.getSomethingElse().getField() == 0) { //do something } somelist.add(something.getSomethingElse().getField()); Do references to the field through getters i...

setters/getters in one method

Just an idea: example (in PHP): to set name: $object->name('name'); to get name: $object->name(); If no argument: the method is used as getter, else as setter. For simple getters/setter. Stupid, whatever, maybe? edit: to follow up on the answers: I don't really like get and set because I prefer to have the interface as explicit as pos...

iPhone Dev - Using a getter in the same class

(Just to let you now, I'm learning to develop for iPhone with a book I got called Beginning iPhone 3 Development: Exploring the SDK, and I do not use Interface builder) Is there ever a reason to use the getter in the same class, when a private member is visible? Like in Foo.h, having NSObject *myObj; ... @property (nonatomic, retain)NSO...

When is my C# property initialized?

I'm a little confused about when exactly my Property is being initialized. Suppose I have a property declared like this: private Dictionary<string, Dictionary<string,string>> MessageLookup { get { return messages ?? doSomething(); } } The doSomething method populates the messages Dictionary ...

Magic __get getter for static properties in PHP

public static function __get($value) does not work, and even if it did, it so happens that I already need the magic __get getter for instance properties in the same class. This probably is a yes or no question, so, it is possible? ...

Setters/Getters, Visibility [ OOP ] in PHP

I'm in the process of reformatting my class ( seen below ) - I think I made the mistake of setting everything with the same visibility in my class, whereas properties should really be private and getters/setters should be public in most cases. To grab a property I just do $path->propertyname but I've noticed its more practical to have s...

does the 'getters and setters are evil' fail for the view layer?

Many people know this article: more on getters and setters. I think it does a convincing job of portraying the evil side of getters/setters. I've also tested it by trying to convert an existing project (unfinished) to code without getters/setters. It worked. Code readability improved greatly, less code and I've even managed to get rid of...

Proper way to modify an NSMutableDictionary instance variable?

Hello! I have an Objective-C NSMutableDictionary declared inside a class's @interface section, with getter/setter methods, like so: @interface myClass : NSObject { NSMutableDictionary *dict; } - (void) setDict: (NSMutableDictionary *) newDict; - (NSMutableDictionary *) dict; Inside some of my @implementation methods I want to mo...

What is the point of setters and getters in java?

Please forgive the length, but here are two programs, both the exact same, but one with and one without setters, getters, and constructors. I've taken a basic C++ class before and don't remember any of these from it, and at the moment I'm not seeing the point of them, if anyone could explain them in lamen's terms I'd much appreciate it....

Generating Indexed Property Getters/Setters in Eclipse

By default, eclipse generates getters/setters according to JavaBeans regular properties style: * public void setName(String name) * public String getName() As of J2SE 5.0 JavaBeans specification allows IndexedPropertyChangeEvents which have a different getter/setter naming scheme for arrays: * public void setName(int index, String n...

Why does a readonly property still allow writing with KVC

I'm working through the "Key Value Coding" chapter in "Programming for Mac OS X". I've built an interface with a slider and a label, both bound to fido, an int. If I set the property for fido to readonly, moving the slider still causes the label to change it's value. I had assumed that I'd get some sort of error for this. If the property...

Why use getters and setters?

What's the advantage of using getters and setters - that only get and set - instead of simply using public fields for those variables? If getters and setters are ever doing more than just the simple get/set, I can figure this one out very quickly, but I'm not 100% clear on how: public String foo; is any worse than: private String fo...

Custom getters in Ruby on Rails

I have a MailingList model that has_may :people For most of my application, I only want to get people that are active So @mailing_list.people should only return people that are active In my model, I can't do def people self.people.find_all{ |p| !p.activated_at.nil? } end because that keeps calling itself. What is the ruby/rails...

accessing struct variable inside getter setter in a c++ class

Okay, I have something like this in C++: class MyClass{ private: int someVariable; int someOtherVariable; struct structName{ int someStructVariable; int someOtherStructVariable; };//end of struct public: //getters & setters for the defined variables. int getSomeStructVariable() { // this does not work I get ...

synthesize getters/setters in AS3

Hi, Is there a way to synthesize getters/setters in AS3? It's very common that you have a class with lots of variables, especially in math calculations (Model in MVC pattern), that you'd like to expose. Is there something like synthesize property in Objective-C, that allows to generate getters/setters? Thanks, Nava ...

Eclipse setting for generating getters and setters insertion point last member

It really annoys me already now... I can not find the setting to have by default insertion point at last member. Why? Generating getters and setters would then be: ALT+SHIFT+S -> space, space, space... -> ENTER :) And not like now: ALT+SHIFT+S -> space, space, space... -> tab, tab, tab, tab, tab, -> up, up, up... -> ENTER ...

Getting and Setting values from Core Data elements in Objective-C?

I've got a simple application with two entities: Person: Attributes: name Relationships: nativeLanguage: (<<-> Language.natives) nonNativeLanguage: (<<-> Language.nonNatives) Language: Attributes: name Relationships: natives: (<->> Person.nativeLanguage) nonNatives: (<->> Person.nonNativeLanguage) On the ed...

benefits of getter/setter VS public vars?

Is there a benifit to using: private var _someProp:String; public function set someProp(value:String):void { _someProp = value; } public function get someProp():String { return _someProp; } As opposed to just using: public var someProp:String; I realise using getter/setter can be useful when you need to further processing ...