Killersites Community: What Problems Does Direct Access To $This->Xxx Cause - Killersites Community

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

What Problems Does Direct Access To $This->Xxx Cause

#1 User is offline   DavidA 

  • View blog
  • Group: New Members
  • Posts: 3
  • Joined: 06-April 13

Posted 08 April 2013 - 02:40 PM

I have read on this site that directly accessing a variable in my class can cause problems. For example the oop tutorial says:

Quote

For example: with the property $name (in object $stefan) you could get its value like so:

$name = $stefan->name;

Though doable, it is considered bad practice to do it because it can lead to trouble down the road. You should use getter methods instead - more on that later.


or in this thread

Quote


Keep in mind, there's little point in using the getters from within the object, so you may as well just use "$this->firstname" rather than "$this->get_firstname()" and make it slightly easier on PHP. The getters are there to access your object's properties from "outside" the object, so you can control how they are accessed or used.


My question is, what problems am I avoiding by creating setter classes? I have a class I'm working on now that just seems bloated by all the getter and setter methods.
0

#2 User is offline   Strider 

  • View blog
  • Group: New Members
  • Posts: 4
  • Joined: 04-March 13

Posted 13 April 2013 - 11:43 AM

The only problem I could probably see is if you are writing something that might conflict with queries or other predefined statements and maybe a person might get it confused with another class that is not extended with extends?

Come to think of it I really can't think of a time that I wrote something like that within public, protected or private function and I can't think of reason why to?

And I don't know why you would be using a lot of getter or setter classes/methods?

This post has been edited by Strider: 17 April 2013 - 07:16 AM

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users