1

我在看几个位置,但不喜欢看,我想执行这个操作

$tpl = new Savant3();
$tpl->Info_.$db_code["name"] = $db_code["info"];
$tpl->display('index.tpl.php');

php echo $this->eprint($this->Info_Name);

可能吗?。谢谢你。

4

2 回答 2

1

你可以简单地做

$tpl->{"Info_$db_code[name]"} = $db_code["info"];

另请参阅手册:http ://www.php.net/manual/en/language.variables.variable.php

于 2014-03-10T07:50:15.637 回答
0

是的,使用变量 variables完全可行。使用包含函数名称的变量来访问类函数。考虑一下:

class testClass {
  function randomstring() {
    return 'hello world!';
  }
}

和这个:

$test = new testClass();

// simulate an arbitrary second half of a string/function name
$random_string = 'string';

// build class name into single variable using arbitrary first half
// of string/function name
$class_name = 'random' . $random_string ;
// use that variable (variable variable)
echo $test->{$class_name}(); // hello world!

在你的情况下:如果这是一个类属性,你会这样做:(如果它是一个函数,添加()

$prop_name = 'Info_' . $db_code["name"];
$tpl->{$prop_name} = $db_code["info"];
于 2014-03-10T07:44:46.000 回答