简体   繁体   中英

What is the use of a variable function in PHP?

I understand how to implement a variable function though i don't understand it's use. Why call a function using a variable than to call the function itself?

Unless to dynamically call functions from user input or returned database results?

EXAMPLE : if you have an input like /?do=something

require_once('do.php');
$fun = 'do_'.$_GET['do'];
if (function_exists($fun)) {
    $fun(); //variable function
} else {
    not_found();
}

so in this case I just add a function to my do.php file and it will be ready to use

do.php :

<?php
function do_getkey() {
    // do something when do=getkey
}
function do_sendkey() {
    // do something when do=sendkey
}
 function not_found() {
    // when not found 
 }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM