PHP: Variable in a function name

后端 未结 7 1873
有刺的猬
有刺的猬 2020-12-09 16:57

I want to trigger a function based on a variable.

function sound_dog() { return \'woof\'; }
function sound_cow() { return \'moo\'; }

$animal = \'cow\';
print soun         


        
7条回答
  •  借酒劲吻你
    2020-12-09 17:23

    You can use $this-> and self:: for class-functions. Example provided below with a function input-parameter.

    $var = 'some_class_function';
    call_user_func(array($this, $var), $inputValue); 
    // equivalent to: $this->some_class_function($inputValue);
    

提交回复
热议问题