PHP 的类内如何使用动态函数名?

2016-07-14 18:19:38 +08:00
 crist

class fmOutput { public function sex($value = '') { return 66; } public function op($funcname='', $value = '') { return $this->$funcname; }

public function __call($funcName,$args)
{
	return "Oops,{{$funcName}}is unexist";
}

} $FM = new fmOutput('sex'); echo $FM->op();

然后,没有任何反应,求各位大神解疑。。

4688 次点击
所在节点    PHP
21 条回复
g079708
2016-07-14 18:53:08 +08:00
g079708
2016-07-14 18:57:00 +08:00
rekulas
2016-07-14 21:57:14 +08:00
return $this->{$funcname}();
wh1012023498
2016-07-14 22:49:37 +08:00
回复支持 md 吗。。

```php
echo 'hello meizi~';
```
wh1012023498
2016-07-14 22:50:23 +08:00
= = 好绝望
UnisandK
2016-07-14 23:02:39 +08:00
@wh1012023498 支持 gist
msg7086
2016-07-15 06:53:07 +08:00
is unexist -> does not exist.

unexist 是什么鬼。
rekulas
2016-07-15 09:09:40 +08:00
@wh1012023498
```
php
echo 'hello meizi~';
```后面不能跟其他字符
```
rekulas
2016-07-15 09:10:10 +08:00
```
ee
```
rekulas
2016-07-15 09:10:28 +08:00
```
代码模式好像不见了 = =
```
crist
2016-07-15 10:03:08 +08:00
@rekulas 这个是正确的,不用加大括号也可以
crist
2016-07-15 10:04:52 +08:00
@msg7086 does not exist 的缩写,你都知道是什么意思啦
msg7086
2016-07-15 10:08:02 +08:00
@rekulas 回复哪来的代码模式?
rekulas
2016-07-15 10:11:10 +08:00
@msg7086 之前貌似是可以的 我看到他们回复过代码
rekulas
2016-07-15 10:12:30 +08:00
@crist 主要是为了规范下 便于理清逻辑 有时候参数不一定是$xx 还有可能是$this->xx 之类的。。
kchum
2016-07-15 11:26:03 +08:00
@rekulas 回复不支持 Markdown
tanteng
2016-07-15 12:00:32 +08:00
$this->{'register'.$command.'Command'}();
rekulas
2016-07-15 13:04:55 +08:00
**gggggggg**
_ggg_
### h1h1
hyq
2016-07-15 14:06:57 +08:00
那是 github 的 gist

https://gist.github.com
techmoe
2016-07-15 23:03:23 +08:00
可变函数后面得加括号吧。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/292541

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX