eclipse 在 php 中为类自动生成 getter 和 setter 的最佳方法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1476419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the best way to auto generate getters and setters for a class in php?
提问by Kennethvr
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.
我经常创建一个包含一些私有变量的类。当设置了这个类的实例时,应该可以用 getter 和 setter 填充该类的所有变量。
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?
有没有一种简单的方法可以做到这一点,而不必在创建类时为每个变量键入所有这些 setter 和 getter?
now i have to type this for each var in the class
现在我必须为班级中的每个 var 输入这个
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?
我们现在使用RegExp,但我不知道这是不是最简单的方法......我们使用Eclipse作为环境,所以也许有一些有用的插件?
回答by erisco
With Eclipse go to Preferences -> PHP -> Editor -> Templates -> New and use something like this:
使用 Eclipse 转到 Preferences -> PHP -> Editor -> Templates -> New 并使用如下内容:
/**
* Method to get the ${PropertyName}
*
* @return ${ReturnType} return the ${PropertyName}.
*
* @since __DEPLOY_VERSION__
*/
public function get${MethodName}() {
return $$this->${PropertyName};
}
/**
* Method to set the ${PropertyName}
*
* @return boolean True on success and false on failed case
*
* @since __DEPLOY_VERSION__
*/
public function set${MethodName}($$value) {
$$this->${PropertyName} = $$value;
}
To use the template type its name and press ctrl+space - a context menu should also automatically appear when you type the name.
要使用模板,请键入其名称并按 ctrl+space - 当您键入名称时,上下文菜单也会自动出现。
回答by Andreas
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.
你看过 __set 和 __get 方法吗?不知道这是否是您的意思,但是只要类成员是 SET 或 Retrieved/Fetched/Accessed 就会自动调用。
回答by Vladislav Rastrusny
Zend Studio has a feature of automatic getter/setter generation.
Zend Studio 具有自动生成 getter/setter 的功能。
回答by RobertPitt
I think the best way is to use the __set and __get with some string functions, Here let me show you.
我认为最好的方法是将 __set 和 __get 与一些字符串函数一起使用,这里让我向您展示。
class UserProfile
{
public function __get($key)
{
if(isset($this->$key))
{
return $this->$key;
}
}
public function __set($key,$val)
{
$this->cahnge($key,$val);
}
public function __call($key,$params)
{
if(substr("set",$key))
{
//Update
}elseif(substr("get",$key))
{
//return
}
//Else Blah
}
private function change($key,$val)
{
if(isset($this->$key))
{
$this->$key = $val;
}
}
}
the __call() method will allow you to set with functions such as
__call() 方法将允许您使用函数进行设置,例如
$profile->setUsername('Robert Pitt');
as long as you substr
the set/get and check for the rest of the string as a value of the class :)
$profile->setUsername('Robert Pitt');
只要您substr
设置/获取并检查字符串的其余部分作为类的值:)
another example
另一个例子
public function __call($method,$params = array())
{
if(isset($params[0]) && is_string($params[0]))
{
$this->$method = $params[0];
}
}
//....
$profile->username('Robert Pitt');
Theres more work to be done here
这里还有更多工作要做
回答by Pawka
Maybe better sollution:
也许更好的解决方案:
class base {
protected $_vars = array();
public function setVar($name, $value = null) {
$this->_vars[$name] = $value;
}
public function getVar($name) {
return isset($this->_vars[$name]) ? $this->_vars[$name] : null;
}
}
And simply extend this class. Or also you can use __set and __get methods, but they are quite slower.
并简单地扩展这个类。或者你也可以使用 __set 和 __get 方法,但它们相当慢。