Are Accessors in Objects Bad Form?
Moderator: General Moderators
Are Accessors in Objects Bad Form?
I here pro's and con's about using accessors. One of the advantages I see is protecting members.
Just curious what others think about this. If not using accessors how do you deal with security?
These articles are a few years old and wandering how they apply today.
Got me curious after reading this OLD forum from 2004
http://discuss.joelonsoftware.com/defau ... 3.15079.11
Then someone in the articles posted this, "Why accessors are evil"
http://www.javaworld.com/javaworld/jw-0 ... olbox.html
Just curious what others think about this. If not using accessors how do you deal with security?
These articles are a few years old and wandering how they apply today.
Got me curious after reading this OLD forum from 2004
http://discuss.joelonsoftware.com/defau ... 3.15079.11
Then someone in the articles posted this, "Why accessors are evil"
http://www.javaworld.com/javaworld/jw-0 ... olbox.html
Re: Are Accessors in Objects Bad Form?
Accessors have nothing to do with security but with code maintainability and encapsulation. In general, if you'd like to read and write directly to a class member, define it as public. If you'd like to make it: A. read only, B. have pre-fetch operation performed on, then use an accessor.
Re: Are Accessors in Objects Bad Form?
Well I guess I mean security in that without accessors properties can be modified directly if they are public even with the proper members. Accessors to be messy and double code. Was wandering how others dealt with this, how others may protect public properties.
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Are Accessors in Objects Bad Form?
There is not one answer to your question. For example, with a ValueObject you want direct access to the properties. With PHP __set() method you can allow direct access and still provide checks for whether to actually set the value.
But perhaps what you have been reading is more about the Law Of Demeter and Tell don't Ask concepts of object design.
But perhaps what you have been reading is more about the Law Of Demeter and Tell don't Ask concepts of object design.
(#10850)
Re: Are Accessors in Objects Bad Form?
Not sure if I am following you.arborint wrote:There is not one answer to your question. For example, with a ValueObject you want direct access to the properties. With PHP __set() method you can allow direct access and still provide checks for whether to actually set the value.
But perhaps what you have been reading is more about the Law Of Demeter and Tell don't Ask concepts of object design.
Code: Select all
class test
{
var $a;
function a($val)
{
if ($a == "")
$a = "10";
}
function __set($func, $val)
{
$this->$func = $val;
}
}
$temp = new test();
$temp->a = "";
Re: Are Accessors in Objects Bad Form?
pytrin wrote:Accessors have nothing to do with security but with code maintainability and encapsulation.
He means code security not network security.
Re: Are Accessors in Objects Bad Form?
visibility != security
Re: Are Accessors in Objects Bad Form?
I don't see how that's relevant to this. This is about accessibility not visibility. Hence 'accessors'. If code outside the scope of the class can't directly manipulate it's properties then it's inherently more secure especially in a dynamically typed environment.
Re: Are Accessors in Objects Bad Form?
Accessors are exactly about class member visibility... If you put accessors for a member, you prevent direct manipulation of it. It's all about the interface, black box approach versus direct manipulation.
Anyway you are arguing semantics, but using visibility decisions on class members will not make your code more secure in the least (and this has nothing to do with PHP being dynamically typed...).
Anyway you are arguing semantics, but using visibility decisions on class members will not make your code more secure in the least (and this has nothing to do with PHP being dynamically typed...).
Re: Are Accessors in Objects Bad Form?
I thought php was loosely typed. 
Re: Are Accessors in Objects Bad Form?
You're still thinking about security as in NETWORK security. I said a couple of posts up I'm talking about code security. It sounds like you haven't worked on large projects with multiple developers. You need code security to stop people manipulating things at the wrong time. I'm not saying all code should have accessors, I NEVER have said that. All I'm saying is accessors with hash tables DO make code inherently more secure.pytrin wrote:Accessors are exactly about class member visibility... If you put accessors for a member, you prevent direct manipulation of it. It's all about the interface, black box approach versus direct manipulation.
Anyway you are arguing semantics, but using visibility decisions on class members will not make your code more secure in the least (and this has nothing to do with PHP being dynamically typed...).
It's both:The Ninja Space Goat wrote:I thought php was loosely typed.
loosley typed=interchangeable datatypes.
dynamically typed=not declaring variables .
Re: Are Accessors in Objects Bad Form?
Don't make assumptions panic, and you are still arguing semantics. If you believe using accessors will make your code more secure from your fellow developers.. than you are dead wrong, and its a very bad attitude. Accessors are about interface, and if you don't know what that means google it up a bit.panic! wrote: You're still thinking about security as in NETWORK security. I said a couple of posts up I'm talking about code security. It sounds like you haven't worked on large projects with multiple developers. You need code security to stop people manipulating things at the wrong time. I'm not saying all code should have accessors, I NEVER have said that. All I'm saying is accessors with hash tables DO make code inherently more secure.