Store function name in database and then execute it

后端 未结 1 667
粉色の甜心
粉色の甜心 2021-01-27 20:13

I am making a kind of CMS system that works in sections of the webpage, and stores each one as a different entry into a table on a database with MySQL. When the user first sets

相关标签:
1条回答
  • 2021-01-27 20:57

    You can use variable functions (http://php.net/manual/en/functions.variable-functions.php).

    $r = mysql_query("SELECT method FROM method_table WHERE id = 2");
    $row = mysql_fetch_assoc($r);
    $func = $row['method'];
    $func($parameter); //will execute whatever method you stored in the `method` field
    

    In this way you can execute a function who's name is stored in a database. If you want to execute it within the context of an object (hence the method), you can do: $this->$func($parameter);

    0 讨论(0)
提交回复
热议问题