What is the better way to write setter and getter in PHP? I mean, is better for a member class to have separate methods for getting and setting its value OR to have only one method for set and get?
Separate methods:
One method:
Why I'm asking this question? There are 2 reasons:
1- I have read at phpGirl a post entitled "Get it? Set it? Good!!" Where she asked the same question.
2- I'm starting to learn C# (Work necessity). In C# there is an elegant way for setter and getter:
So now if t is an instance of test, t.Count gets the value of the count member and t.Count = 10 sets the count member to 10. Just one entity to set and to get.
In PHP, I have always written 2 separate methods for setting and getting, but I think the way C# do it is elegant, although one method for both Set and Get in PHP is not wise. I think most PHP developers have to change their way of thinking to familiarize them selves with the "One Method" way if they want to go this direction. My personally preference in PHP is 2 methods which, I think, is the best suited for PHP-language. I haven't seen as I remember a PHP developer do it the "One Method" way.
What do you think ?
Separate methods:
class test {
private $count;
public function setCount( $value )
{
$this->count = $value;
}
public function getCount()
{
return $this->count;
}
}One method:
class test {
private $count;
public function Count( $value = "" )
{
if( empty( $value ) )
return $this->count;
else
$this->count = $value;
}
}Why I'm asking this question? There are 2 reasons:
1- I have read at phpGirl a post entitled "Get it? Set it? Good!!" Where she asked the same question.
2- I'm starting to learn C# (Work necessity). In C# there is an elegant way for setter and getter:
class test {
private int count;
public int Count
{
set { count = value; }
get { return count; }
}
}So now if t is an instance of test, t.Count gets the value of the count member and t.Count = 10 sets the count member to 10. Just one entity to set and to get.
In PHP, I have always written 2 separate methods for setting and getting, but I think the way C# do it is elegant, although one method for both Set and Get in PHP is not wise. I think most PHP developers have to change their way of thinking to familiarize them selves with the "One Method" way if they want to go this direction. My personally preference in PHP is 2 methods which, I think, is the best suited for PHP-language. I haven't seen as I remember a PHP developer do it the "One Method" way.
What do you think ?
07 Mar 2006 22:10:26
What about this http://www.dgx.cz/trine/ite... ?
19 Jan 2007 15:56:41
Hey cool site... if you can use PHP 5, it has two magic methods called __get() and __set()
which you can use like the following...in this case its easiest to internally use an array to store the values, but you can also use regular variables as below (this code isnt tested sorry may be bugs). Even easier than c# properties!
class Test {
private $var1;
private $var2;
public function __set($key,$val) {
$this->$key=$val;
}
public function __get($key) {
return $this->$key;
}
}
30 Apr 2007 11:46:49
John, good comment. but is there any way to make encapsulation code using __set or __get in terms some time there should be specific procedures to do during setting property or getting one?
30 Apr 2007 11:54:47
I though we can combine with allowed-empty-parameter-function as code below
class MyClass
{
private $prop1;
private $prop2;
public function __set($key, $val)
{
if ($key == "prop1")
Prop1Handler($val);
else
$this->$key = $val;
}
public function __get($key)
{
if ($key == "prop1")
return Prop1Handler();
else
return $this->$key;
}
private Prop1Handler($val == "")
{
if (empty($val))
return $this->prop1;
else
$this->prop1 = $val;
}
}
28 Aug 2007 00:47:42
Hello. I'm brazillian and now knowed this blog.And, my english is vary bad. hehehe.
I maked a function for getter and setter in my class. I called of "super magic getters/setters" because is used __get / __set with auto reflection to discovery your attributes of class.
see belong:
//SUPER GETTER MÁGICO
public function __get($property) {
if(array_key_exists($property,get_class_vars(__CLASS__))) {
return $this->$property;
} else {
die("<p>Método Getter inexistente da classe ".__CLASS__."<br>");
}
}
//SUPER SETTER MÁGICO
public function __set($property, $value) {
if (array_key_exists($property, get_class_vars(__CLASS__))) {
$this->$property = $value;
} else {
die("Método Setter inexistente da classe ".__CLASS__."<br>");
}
}
simple. But, for especific datas, I recommend metods separateds.
T+ (see later)
Mario junior
juninhog12||at||gmail||dot||com