什么是自动生成getter和setter为php中的类的最好的方法? [英] What is the best way to auto generate getters and setters for a class in php?

查看:1296
本文介绍了什么是自动生成getter和setter为php中的类的最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常创建一个有几个私有变量的类。
当设置这个类的实例时,应该可以使用getter和setter来填充类的所有变量。

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.

有一个简单的方法在创建类时,不必为每个变量键入所有这些setter和getter?

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?

现在我必须为类中的每个var键入

now i have to type this for each var in the class

public function setVar($var){
$this->var = $var;
}

public function getVar(){
return $this->var;
}



我们现在使用RegExp,但我不知道这是否是最简单的way ...
我们使用Eclipse作为环境,所以也许有一些有用的插件?

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?

推荐答案

到首选项 - > PHP - >编辑器 - >模板 - >新建并使用像这样:

With Eclipse go to Preferences -> PHP -> Editor -> Templates -> New and use something like this:

private $$${PropertyName};
${cursor}    
public function get${PropertyName}() 
{
  return $$this->${PropertyName};
}

public function set${PropertyName}($$value) 
{
  $$this->${PropertyName} = $$value;
}

要使用模板类型的名称并按ctrl + space - 应该也会在您输入名称时自动出现。

To use the template type it's name and press ctrl+space - a context menu should also automatically appear when you type the name.

这篇关于什么是自动生成getter和setter为php中的类的最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆