我正在使用这个php库
fzaninotto/Faker
<?php
// require the Faker autoloader
require_once '/path/to/Faker/src/autoload.php';
// use the factory to create a Faker'Generator instance
$faker = Faker'Factory::create();
$name=$_POST['name'];
switch ($name) {
case 'text':
echo $faker->text;
break;
case 'name':
echo $faker->name;
break;
case 'address':
echo $faker->address;
break;
default:
echo $faker->anything;
break;
}
?>
AS 你可以看到调用"faker"
的任何方法或属性我需要使用开关语句。
有没有更好的做事方法??
<form action="index.php" method="post">
<input type="text" name="name" value="text" /><br><br>
<input type="submit" value="Run" />
</form>
我想做的是这样的
<?php
$name=$_POST['name'];
echo $faker->$name;
//So if user type text
echo $faker->text ;
//or if user type "words($nb = 3, $asText = false)"
echo $faker->words($nb = 3, $asText = false);
?>
<?php
$userinput='paragraph|{"nb":"10","text":"true"}';
$explode=explode('|',$userinput);
$functionName=$explode[0];
$arg=json_decode(end($explode),true);
echo call_user_func_array(array($this->faker, $functionName), $arg);
?>