I regularly create a class that has a few private variables. When an instance of this class is set, it should be possible to fill all variables of the class with getters and setters.
Is there an easy way to do this without having to type all these setters and getters for each variable on creation of the class?
now i have to type this for each var in the class
public function setVar($var){
$this->var = $var;
}
and
public function getVar(){
return $this->var;
}
We now use RegExp, but i don't know if this is the easiest way... We use Eclipse as environment, so perhaps there are some usefull plugins?
Maybe better sollution:
And simply extend this class. Or also you can use __set and __get methods, but they are quite slower.
With Eclipse go to Preferences -> PHP -> Editor -> Templates -> New and use something like this:
To use the template type it's name and press ctrl+space - a context menu should also automatically appear when you type the name.
I think the best way is to use the __set and __get with some string functions, Here let me show you.
the __call() method will allow you to set with functions such as
$profile->setUsername('Robert Pitt');
as long as yousubstr
the set/get and check for the rest of the string as a value of the class :)another example
Theres more work to be done here
Have you looked at the __set and __get methods? Don't know if this is what you mean but the are automatically called whenever a class member is SET or Retrieved/Fetched/Accessed.
Zend Studio has a feature of automatic getter/setter generation.