我在看几个地方,但不喜欢看,我想执行这个操作
$tpl = new Savant3();
$tpl->Info_.$db_code["name"] = $db_code["info"];
$tpl->display('index.tpl.php');
php echo $this->eprint($this->Info_Name);
是可能的吗?谢谢你。
你可以直接做
$tpl->{"Info_$db_code[name]"} = $db_code["info"];
请参阅手册:http://www.php.net/manual/en/language.variables.variable.php
是的,使用可变变量完全可行。使用包含函数名的变量来访问类函数。想想看:
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"];