一种方法中的设置器/获取器

发布于 2024-07-29 18:35:42 字数 302 浏览 2 评论 0原文

只是一个想法:

示例(PHP): 设置名称: $object->name('name'); 获取名称: $对象->名称();

如果没有参数:该方法用作 getter,否则用作 setter。 对于简单的 getter/setter。 愚蠢,无论如何,也许?

编辑:跟进答案:我不太喜欢获取和设置,因为我更喜欢让界面尽可能明确。 当只有少数属性时,恕我直言,这也太过分了。 所以我想将其范围缩小到具有几个显式 getter/setter 的类/对象。

Just an idea:

example (in PHP):
to set name:
$object->name('name');
to get name:
$object->name();

If no argument: the method is used as getter, else as setter. For simple getters/setter. Stupid, whatever, maybe?

edit: to follow up on the answers: I don't really like get and set because I prefer to have the interface as explicit as possible. When there are only a few properties it's also overkill IMHO. So I'd like to narrow it down to classes/objects with a couple of explicit getters/setters.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小ぇ时光︴ 2024-08-05 18:35:44

可以使用 __call() 魔术方法来完成。

class Test {
    public function __call($name, array $args) {
        $variable =& $this->$name;
        if(!empty($args)) {
            $variable = $args[0];
        }
        return $variable;
    }
}

It can be done using the __call() magic method.

class Test {
    public function __call($name, array $args) {
        $variable =& $this->$name;
        if(!empty($args)) {
            $variable = $args[0];
        }
        return $variable;
    }
}
若沐 2024-08-05 18:35:44

当然,如果它在您的应用程序中有意义,您可以这样做,否则我只会使用已经为您设置的标准 getter/setter。 你的函数可能看起来像这样:

public function name($val = null)
{
  if (is_null($val))
  {
    return $this->name;
  }
  else
  {
    $this->name = $val;
  }
}

Sure, you could do that if it makes sense in your application, otherwise I would just use the standard getters/setters which have already been set up for you. Your function could look something like this:

public function name($val = null)
{
  if (is_null($val))
  {
    return $this->name;
  }
  else
  {
    $this->name = $val;
  }
}
×眷恋的温暖 2024-08-05 18:35:43

问题是很难遵循。 我更愿意使用 PHP5 的 __get 和 __set,这样获取和设置变量会更自然,而且每个人都会确切地知道我在做什么。 IE:

class myClass
{

    function __get($name)
    {
       return $this->array[$name];
    }
    function __set($name, $value)
    {
       $this->array[$name] = $value;
    }
    function print()
    {
       echo $this->array['test'];
    }
}
$obj = new myClass;
$obj->test = "Hi";
echo $obj->test; //echos Hi.
$obj->print(); //echos Hi.

The problem is it would be hard to follow. I'd much rather use PHP5's __get and __set so it is more natural to get and set variables, and everyone would know exactly what I am doing. IE:

class myClass
{

    function __get($name)
    {
       return $this->array[$name];
    }
    function __set($name, $value)
    {
       $this->array[$name] = $value;
    }
    function print()
    {
       echo $this->array['test'];
    }
}
$obj = new myClass;
$obj->test = "Hi";
echo $obj->test; //echos Hi.
$obj->print(); //echos Hi.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文