How can you access object properties from an object method?

What is a “purist” or “correct” way to access object properties from an object method that is not a getter / setter method?

I know that from the outside of the object you should use getter / setter, but from the inside you would only do:

Java:

String property = this.property; 

PHP:

 $property = $this->property; 

or would you do:

Java:

 String property = this.getProperty(); 

PHP:

 $property = $this->getProperty(); 

Forgive me if my Java is a bit off, it has been a year since I programmed Java ...

EDIT:

People seem to suggest that I'm only talking about private or protected variables / properties. When I recognized OO, I was taught how to use getters / setters for every single object, even if it was publicly available (and in fact I was told never to publish any variable / property). So, I can start with a false assumption from the very beginning. It seems that the people answering this question can say that you should have public properties and that they don’t need getters and setters, which contradicts what I was taught and what I talked about, although perhaps it should be discussed like Well. This is probably a good topic for another question, though ...

+79
java oop php theory
Aug 01 '08 at 16:10
source share
18 answers

This has the potential of a religious war, but it seems to me that if you use a getter / setter, you should also use it internally - using both of them will lead to maintenance problems in the future (for example, someone adds code to the setter, which must be run every time when this property is set and the property is set internally without calling the calling device).

+58
Aug 01 '08 at 16:13
source share

Personally, I feel it is important to stay consistent. If you have recipients and setters, use them. The only time I got access to the field directly is when the accessory has a lot of overhead. It may seem like you are inflating your code unnecessarily, but it can certainly save a lot of headache in the future. Classic example:

Later you can change the way this field works. Perhaps it should be designed on the fly, or maybe you want to use a different type for backup storage. If you access properties directly, such a change can break a lot of code in a single foop wave.

+42
Aug 01 '08 at 18:23
source share

I am quite surprised how unanimous that getters and setters are good and good. I suggest Allen Golub's incendiary article, Getters And Setters Evil . Of course, the name has the meaning of shock, but the author makes the right points.

Essentially, if you have getters and setters for each private field, you make these fields as good as public. It will be very difficult for you to change the type of private field without ripple effects for each class that this getter .

In addition, from a strictly OO point of view, objects must respond to messages (methods) that correspond to their (I hope) common responsibility. The vast majority of getters and setters do not make sense for their component objects, Pen.dispenseInkOnto(Surface) makes more sense to me than Pen.getColor() .

Getters and seters also encourage class users to request an object for some data, perform calculations, and then set a different value in the object, better known as procedural programming. You better just tell the object to do what you are going to do in the first place; also known as Information Expert .

Getters and seters, however, are necessary evils at the layer boundary - UI, persistence, etc. Limited access to internal class classes, such as the C ++ friendship keyword, secure access to Java packages, .NET internal access and the friends class template can help you reduce the visibility of getters and configure only those who need them.

+25
Sep 19 '08 at 0:13
source share

It depends on how the property is used. For example, let's say you have a student object that has a name property. You can use the Get method to retrieve the name from the database if it has not yet been received. Thus, you reduce unnecessary calls to the database.

Now let's say that you have a private integer counter in your object that counts the number of times the name was called. You may not use the Get method from within the object, because this will result in an invalid count.

+18
Aug 01 '08 at 16:19
source share

PHP offers many ways to handle this, including the magic methods __get and __set , but I prefer explicit getters and seters. That's why:

  • Validation can be placed in setters (and recipients, for that matter)
  • Intellisense works with explicit methods
  • It’s not a question of whether the property is read-only, write-only or read-write
  • Getting virtual properties (i.e. computed values) looks just like regular properties
  • You can easily set an object property that is never defined anywhere, which is then not documented
+13
Sep 24 '08 at 17:24
source share

Am I just going overboard here?

Maybe;)

Another approach would be to use a private / protected method to actually get (caching / db / etc) and a public shell for it, which increments the counter:

PHP:

 public function getName() { $this->incrementNameCalled(); return $this->_getName(); } protected function _getName() { return $this->name; } 

and then from the object itself:

PHP:

 $name = $this->_getName(); 

So you can use this first argument for something else (for example, sending a flag to use cached data here or not).

+12
Aug 01 '08 at 16:43
source share

I must be missing the point here, why are you using a getter inside an object to access a property of this object?

Taking this to its end, the getter must call the getter, which should call the getter.

So, I would say that inside a method of an object, a property is directly opened, especially if you see that you call another method in this object (which will simply access the property directly and then return it) - this is just a meaningless wasteful exercise (or I there is a misunderstood question).

+11
Jun 04 2018-11-11T00:
source share

If "purist" means "large encapsulation", then I usually declare all my fields private, and then use this.field from the class itself, but all other classes, including subclasses, access the state of the instance using getters.

+7
Aug 22 '08 at 11:15
source share

I would say that it is better to use access methods even inside the object. Here are the points that come to my mind right away:

1) This should be done in the interests of maintaining consistency with appeals made outside the facility.

2) In some cases, these access methods can do more than just access the field; they may perform some additional processing (although this is rare). If so, with direct access to the field you would miss this additional processing, and your program could go awry if this processing will always be performed during these calls

+7
Jan 20 '10 at 8:32
source share

I found that using setters / getters made it easier to read code. I also like the control it gives when other classes use methods, and if I change the data that the property will store.

+6
Aug 18 '08 at 17:37
source share

Private fields with public or protected properties. Access to the values ​​must go through the properties and be copied to a local variable if they will be used more than once in the method. If and ONLY, if you have the rest of your application completely changed, swung and otherwise optimized so that accessing values ​​through their associated properties becomes a bottleneck (and this will never happen, I guarantee), if you even begin to consider including any other properties besides properties, directly relate to their reference variables.

.NET developers can use automatic properties for enforcement, since you cannot even see support variables during development.

+6
Aug 18 '08 at 17:43
source share

If I do not edit the property, I will use the public get_property() method, if this is not a special case, such as a MySQLi object inside another object, in which case I will simply publish its property and name it $obj->object_property .

Inside an object, it always has the $ this-> property for me.

+6
Aug 22 '08 at 11:34
source share

The purist way of OO is to avoid both and follow the Law of Demeter with Tell Do not Ask .

Instead of getting the property value of an object that tightly connects two classes, use this object as a parameter, for example.

  doSomethingWithProperty() { doSomethingWith( this.property ) ; } 

If the property was a native type, for example. int, use the access method, name it for the problem domain, not for the programming domain.

  doSomethingWithProperty( this.daysPerWeek() ) ; 

This will allow you to support encapsulation and any post-conditions or dependent invariants. You can also use the setter method to maintain any preconditions or dependent invariants, however, do not fall into the trap of calling them setters, return to the Hollywood principle for naming when using the idiom.

+6
Sep 24 '08 at 17:04
source share

It depends. This is more a style issue than anything else, and there is no hard and fast rule.

+6
Oct 01 '08 at
source share

Well, it looks like the C # 3.0 properties implementation is completed by default, the decision is made for you; you need to set a property using a (possibly private) set of properties.

I personally use only the private member when it does not, will cause the object to fall in a less desirable state, for example, during initialization or when using caching / lazy loading.

+5
Aug 01 '08 at 16:56
source share

I may be wrong because I am autodidact, but I NEVER published custom properties in my Java clans, they are always closed or protected, so the external code must be accessed using getters / seters. This is better for maintenance / modification purposes. And for the inner class code ... if the getter method is trivial, I use this property directly, but I always use setter methods, because I could easily add code to fire events if I want

+5
Aug 6 '08 at 14:43
source share

I like cmcculloh's answer, but Greg Hurlman's answer seems to be the most correct. Use getter / seters all the time if you started using them from getgo and / or used to work with them.

As an aside, I personally believe that using getter / seters makes the code easier to read and debug later.

+5
Sep 15 '08 at 9:46
source share

As indicated in some comments: Sometimes you need, sometimes you should not. Most of the private variables are that you can see all the places where they are used when you change something. If your getter / setter does the right thing, use it. If it doesn’t matter what you decide.

The opposite case may be that if you use a getter / setter, and someone changes the getter / setter, they have to analyze all the places where the receiver and setter are used internally to find out if something is hindering.

+4
Aug 01 '08 at 17:01
source share



All Articles