Reputation: 18607
I'm looking for advice/experience on using public variables vs private variables with accessor methods in php.
eg $obj->foo = 'a'; echo $obj->foo;
vs $obj->setFoo('a'); echo $obj->getFoo();
What I like about public variables is the shorter syntax - just seems less work to use. I understand that it could make refactoring more difficult later, but I've never experienced it (meaning, sometimes the design changes - but usually the accessor methods would need to be changed any.)
The other option is to store the variables in an array and use magic methods (__get/__set) to access them - then I have the ease of use of public variables with the ability to refactor or accessor methods.
Any experience or references of what people do in the php world.
And for anyone that hold the accessor method are the best way, is there a valid need/use for public variables?
Upvotes: 1
Views: 2052
Reputation: 135
Setters and Getters are one kind of implementation for encapsulation, one of the OOP principles, which says an object must be a black box just exposing behaviors and its state to its clients, but preventing the exposure of its internal. This allows low coupling code as you can change the logic inside a Setter and / or Getter without affecting the client code using your API.
As many other have stated before, there are other implementations such as magic methods.
Upvotes: 0
Reputation: 145482
Getters/Setters arose in Java. They have no place in scripting languages, hence they are frowned upon in e.g. Python and Javascript. They make the API more combersome.
As you already know, you can use __get to wrap variable access. This is advisable in scripting languages. It allows you to e.g. validate types in a central location, you don't have duplicated code in multiple setters/getters.
function __set($name, $value) {
if (gettype($value) == $this->_types[$name]) {
$this->_vars[$name] = $value;
}
} // instead of twenty methods checking the type
It's a code smell to implement hollow getters/setters to have enterprisey-looking code. And it's also inadvisable to have getters/setters with side-effects. Always use real methods / messages for complex object operations.
Upvotes: 0
Reputation: 57268
setters and getters are always the best option because they provide more stability for validation / sanitization.
public function __call($name,$params = array())
{
if(strstr('set',$name))
{
}//returns
if(strstr('get',$name))
{
}//Returns
if(strstr('run',$name))
{
}//Returns
}
Upvotes: 0
Reputation: 212412
Yes, accessor methods are an overhead, although the syntax for using setters/getters is just as clean as direct access of public properties... slightly more wordy, but just as clean.
Biggest benefit of accessor methods is that you can use your set method to validate values and reject inappropriate values (e.g. trying to set a property that should always be an integer to a string or an array)... but this verification only works if external code can't access the property directly.
Second benefit if your code has related properties, where a change to property A requires a change to property B as well... if the properties are public, you can't control (or enforce) this.
Using set methods allows you to implement fluent interface, or to cleaner code for instances like:
echo $obj->setFoo('a');
instead of
$obj->setFoo('a'); echo $obj->getFoo();
if you include a return in your set method
Upvotes: 7