in C#, getters and setters can be defined and filled differently for each attribute (property) but in php it looks a common gateway for all the attributes. Is there is a way to define getters and setters for each properties like the code shown below in C#:
private string hello;
private string world;
public string Hello
{
get{ return hello;}
set{ hello = value + "this one is hello";
}
public string World
{
get{return world;}
set{world = value + "this one is world";
}
What I want is a similar way to define different getters and setters for some particular attributes.
Edit:
I know a way but they needs function calls like set_attr1($value){} and get_attr1(){} and declare private $attr1 but as you may see, it is not quite what I want. I want them invoked automatically when I reach $attr1
Thanks.
You could set a magic method with a switch case, but that won’t fix your issue at 100%, because the magic method will not get invoked unless the property does not exist or if its scope is not accessible from the caller:
You can hack around this, by prepending your properties name with, say, an underscore. But this still feel very much like a hack:
In conclusion, I think you’re better off defining indiviual setters/getters method for each property to remove any ambiguity whatsoever: