根据属性的值调用方法(Call a method based on the value of a property)

编程入门 行业动态 更新时间:2024-10-08 12:38:00
根据属性的值调用方法(Call a method based on the value of a property)

我根据属性的值调用了大量方法。 我想知道是否有更正确的方法来做我正在做的事情?

我的代码现在:

if ($qvalue->fid == 1) { $this->Archive1(); } if ($qvalue->fid == 2) { $this->Archive2(); } if ($qvalue->fid == 3) { $this->Archive3(); } if ($qvalue->fid == 4) { $this->Archive4(); } if ($qvalue->fid == 5) { $this->Archive5(); } if ($qvalue->fid == 6) { $this->Archive6(); } if ($qvalue->fid == 7) { $this->Archive7(); } if ($qvalue->fid == 8) { $this->Archive8(); } if ($qvalue->fid == 9) { $this->Archive9(); } if ($qvalue->fid == 10) { $this->Archive10(); } ...

我可以这样做:

$this->Archive($qvalue->fid)();

我知道这不起作用,我只是不确定是否有可能做得更好。 甚至不确定要搜索什么类似的其他帖子。

I have a large list of methods that I call based on the value of a property. I was wondering if there is a more correct method todo what I am doing?

My code now:

if ($qvalue->fid == 1) { $this->Archive1(); } if ($qvalue->fid == 2) { $this->Archive2(); } if ($qvalue->fid == 3) { $this->Archive3(); } if ($qvalue->fid == 4) { $this->Archive4(); } if ($qvalue->fid == 5) { $this->Archive5(); } if ($qvalue->fid == 6) { $this->Archive6(); } if ($qvalue->fid == 7) { $this->Archive7(); } if ($qvalue->fid == 8) { $this->Archive8(); } if ($qvalue->fid == 9) { $this->Archive9(); } if ($qvalue->fid == 10) { $this->Archive10(); } ...

Can I do something like this:

$this->Archive($qvalue->fid)();

I know this wont work, I just am not sure if something better is possible. Not even sure what to search to see other posts similar.

最满意答案

$method = 'Archive' . $qvalue->fid; if (method_exists($this, $method)) { $this->$method(); } $method = 'Archive' . $qvalue->fid; if (method_exists($this, $method)) { $this->$method(); }

更多推荐

本文发布于:2023-07-19 15:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1182559.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   方法   Call   property   based

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!