仅仅是我的好奇心,我有一个有
的课程class someCLass {
var $_var1 = '';
var $_var2 = '';
public function _set(){}
public function _get(){}
public function _put(){}
}
是否可以动态调用此函数。例如:
public function insomefunc(){
$key_sample = 'set';
$result = $this->_$keysample(); //call dynamically a function which should be _set()
}
变量
的方式相同public function insomefunc(){
$var_sample = 'var1';
$this->_$varsample = 'jackpot' //assign
}
想知道启发的答案。谢谢
答案 0 :(得分:1)
您必须在字符串中添加“_”:
public function insomefunc(){
$key_sample = 'set';
$result = $this->{'_'.$keysample}(); //call dynamically a function which should be _set()
}
请参阅http://php.net/manual/en/functions.variable-functions.php
答案 1 :(得分:0)
你可以这样做..
<?php
class someCLass
{
var $_var1 = '';
var $_var2 = '';
public function set()
{
echo "I am set";
}
public function get()
{
}
public function put()
{
}
public function runset()
{
$key_sample = 'set';
$this->$key_sample();
}
}
$a = new someCLass();
$a->runset();
<强> OUTPUT :
强>
I am set