企业架构师中的魔术入门者

企业架构师中的魔术入门者

本文介绍了企业架构师中的魔术入门者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Enterprise Architect制作一个UML类图并使用它生成PHP5代码。使用,可以为属性创建吸气剂和设置器(在代码中看起来像这样)(仅显示相关行) :

  private $ id; 

公共功能getId()
{
return $ this-> id;
}

/ **
*
* @param newVal
* /
公共函数setId($ newVal)
{
$ this-> id = $ newVal;
}

我想使用魔术方法 __ get ($ property) __ set($ property,$ value),而不是为每个属性使用单独的方法。有可能,如果可以,如何?



对于吸气剂,它看起来像这样:

  public函数__get($ property)
{
switch($ property){
case'id':return $ this-> id;打破;
默认值:返回null;
}
}


解决方案

I同意此页面上的其他人的做法,这是不正确的做法,您应该坚持使用简单的老方法和方法来使用魔术方法。一旦需要在访问器/更改器中进行验证或其他计算,您的开关/外壳就会爆炸。维护很麻烦,而且继承不完全友好。具有魔术方法的子类在技术上会覆盖父级的魔术方法。



但是,您可以通过使用EA的代码模板编辑器修改代码模板来实现。 / p>

引用自:

还有:

带有一些模板的代码编辑器的示例图片:





我对编辑器及其使用的模板语言不熟悉,因此无法为您提供有效的示例。但是我想如果您真的想修改模板,可以从那里弄清楚。


I'm using Enterprise Architect to make a UML class diagram and generate PHP5 code with it. Using this, one can make getters and setters for an attribute, which looks like this in the code (only relevant lines shown):

private $id;

public function getId()
{
    return $this->id;
}

/**
 *
 * @param newVal
 */
public function setId($newVal)
{
    $this->id = $newVal;
}

I'd like to use the magic methods __get($property) and __set($property, $value) instead of seperate methods for each property. Is it possible, and if so, how?

It could look like this, for the getter:

public function __get($property)
{
    switch ($property) {
        case 'id': return $this->id; break;
        default: return null;
    }
}
解决方案

I agree with other people on this page that it's bad practise and you should stick to plain old getters and setters over the magic methods. Once you need validation or other computations in the accessors/mutators your switch/case will explode. It's a mess to maintain and not exactly inheritance friendly. A child class with the magic methods is technically overwriting the parent's magic method.

However, you can do that by modifing the Code Templates with the Code Template Editor of EA.

Quoting from Enterprise Architect User Guide on Code Templates:

and further:

Here is a sample picture of the Code Editor with some template loaded into it:

I am not familiar with the Editor nor the template language they use, so I cannot provide you with a working example. But I guess you can figure it out from there if you really want to modify the template.

这篇关于企业架构师中的魔术入门者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 05:25