Translating a Mysql Database Record Value into a PHP command
我有一个字段数据库记录行,其内容如下:
1 2 3 | | id | command | ------------------ | 1 | getName() | |
我还有一个php实体,它是用户,具有
假设数据库记录被读取并解析为$record,而我有$user,那么如何将$record与$user命令连接起来?
我试过
代码示例:
1 2 3 4 5 | $record = DB::queryFirstRow("SELECT * from command_records WHERE id='1'"); $user = new User("Kenny"); // Here is where I'm trying to perform the $user->getName() // by reading $record['command']. $record['command'] is getName() $user-> . $record['command']; |
号
当然失败了。我正在考虑避免早点谷歌搜索,但也没办法解决。
您要查找的是PHP的变量
您可以尝试如下操作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class User { public function getName() { return"SomeName"; } } // assuming this is how your result looks like $result = [ 'command' => 'getName()', // more data ]; $command = $result['command']; // is"getName()" $command = str_replace('()','',$command); // is"getName" // you need to remove the '()'. just get the name of the method $user = new User(); echo $user->{$command}(); // output: SomeName |
请访问:https://3v4l.org/ovyaa