2010-09-04 4 views
2
<?php 
    $str = "getList"; 

    //now by doing something to $str i need to call getList() method any sugesstions 

    function getList(){ 
    echo "get list called"; 
    } 
?> 

Répondre

1

Vous pouvez utiliser la variable comme nom de fonction. Cela exécutera getList():

$str(); 

Cependant, des choses comme cela est souvent un symptôme d'un problème de conception. Vous voulez élaborer ce dont vous avez besoin?

5

Utilisez la fonction call_user_func() pour appeler la fonction par son nom.

+0

thanqu très bien .. ce résolu mon problème – Sekhar

4

Cette caractéristique est connue sous le nom Variable functions, voici un exemple de php.net:

<?php 
function foo() { 
    echo "In foo()<br />\n"; 
} 

function bar($arg = '') 
{ 
    echo "In bar(); argument was '$arg'.<br />\n"; 
} 

// This is a wrapper function around echo 
function echoit($string) 
{ 
    echo $string; 
} 

$func = 'foo'; 
$func();  // This calls foo() 

$func = 'bar'; 
$func('test'); // This calls bar() 

$func = 'echoit'; 
$func('test'); // This calls echoit() 
?> 

Plus d'info: