简体   繁体   English

PHP动态函数调用

[英]PHP dynamic function call

I'm not so sure about how I should ask this question but I'll try to make it clear : 我不太确定如何提出这个问题,但我会尽力弄清楚:

I have document.php and doc2.php (for example). 我有document.php和doc2.php(例如)。

In the first one, I want to put a function in a variable so that I'll be able to say in the first one : 在第一个中,我想将一个函数放在变量中,这样我就可以在第一个中说:

   $_SESSION['action'] = 'exampleFunction($params)';

In the second document, I'd love to be able to say : 在第二份文件中,我很想说:

   $params = 'test';
   $_SESSION['action']; 

It would then be possible for me to launch the right function depending on what the user does in the previous document. 然后,根据用户在先前文档中的操作,我可能会启动正确的功能。

You can use "variable functions" 您可以使用“变量函数”

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

$func = 'foo';
$func('arg1');

Reference: http://php.net/manual/en/functions.variable-functions.php 参考: http : //php.net/manual/en/functions.variable-functions.php

Or you can use create_function() . 或者,您可以使用create_function()

Reference. 参考。

You can use eval function: http://www.php.net/manual/function.eval.php 您可以使用eval函数: http : //www.php.net/manual/function.eval.php

eval($_SESSION['action']);

But i strongly discourage to do so, because it's dangerously insecure 但是我强烈不鼓励这样做,因为这是不安全的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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