【发布时间】:2014-04-16 09:55:25
【问题描述】:
我看到 getter 和 setter 代码已在我们的 PHP 应用程序中作为隐式事实标准实现,其效果是项目类中的每个实例变量都可能是公共的。
我不想在每次打开类时都看到代码中每个实例变量的 getter 和 setter 列表。它们通常是我第一眼看到的东西,而且它们的长度通常超过了除了设置和获取之外的类的核心内容,所以我必须像布偶一样在 getter 和 setter 的海洋中花一分钟滚动,尤其是当它们像这样排列时:
$this->getter();
$this->setter();
$this->actualstuff();
$this->getter();
$this->setter();
$this->getter();
$this->getter();
$this->setter();
$this->actualstuff();
$this->getter();
$this->setter();
当我在临终前,我会意识到我的一半生命将浪费在寻找 setter 和 getter 上,所以我试图呼吁我的同事不要在习惯性地完成实例变量后自动生成它们并且毫无意义地依赖它们。
这里的问题是我的同事喜欢他们出现在 IDE(我们使用 PHPstorm)代码完成中,这使得输入时更容易。
我想知道是否有一种技术可以不使用魔法方法来完成 getter 代码。也许是一个 phpstorm 插件或 PHP 类。
class Product{
private $price;
private $amount;
public function toGetter(){
$return = array();
$return['price'] = $this->price;
$return['amount'] = $this->amount;
$getter = new GetterObject($return); //Hypothetical solution class
return $getter;
}
}
$Product = new Product();
$Product->toGetter()->getPrice(); //appears in PHPstorm code completion
我会接受任何其他类型的建议。
【问题讨论】:
-
使用
@method和__call可能吗?魔术——是的,但是您可以在 PHPDoc 类的注释中提前描述所有这些。至于您的代码示例——请提供GetterObject类的代码。 -
GetterObject 是假设的,它是我想要实现的。
-
我会检查@method/__call out
-
至于将 getter/setter 与实际内容混合——使用代码重新排列功能(按字母顺序对类方法进行排序,同时保持 getter 和 setter 彼此相邻)——
Settings | Code Style | PHP | Arrangement。结合自定义折叠区域,您可以轻松隐藏所有不重要的内容。 -
+ 1 不知道!
标签: php phpstorm getter-setter