dpvmjk0479 2013-12-19 03:59
浏览 134
已采纳

处理$ _POST数据时遇到array_map问题

I'm working on updating some code that calls a function based on post data. The original class looks like this:

class RegistrationProceduresController
{
    public function __construct($username, $password, $host, $port, $dbname)
    {
        $SP = new RegistrationProceduresModel($username, $password, $host, $port, $dbname);

        if(method_exists($SP, $SP->$_POST['function']()))
        {
            $SP->$_POST['function']();
        }
        else
        {
            die("Function does not exist" . isnull($SP->$_POST['function'](), ". No function was specified"));
        }
    }
}

I'm trying to update this class to sanitize the data before executing a function based on post data. So far I've come to the following:

class RegistrationProceduresController
{
    public function __construct($username, $password, $host, $port, $dbname)
    {
        $SP = new RegistrationProceduresModel($username, $password, $host, $port, $dbname);

        // Sanitize all the incoming data
        $sanitized = array_map('sanitize', $_POST);

        if(method_exists($SP, $SP->$sanitized['function']()))
        {
            $SP->$sanitized['function']();
        }
        else
        {
            die("Function does not exist" . isnull($SP->$sanitized['function'](), ". No function was specified"));
        }
    }

    public function sanitize($input)
    {
        return htmlspecialchars(trim($input));
    }
}

This brings me to the following:

Warning: array_map() expects parameter 1 to be a valid callback, function 'sanitize' not found or invalid function name in C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php on line 17Fatal error: Method name must be a string in C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php on line 19

Which is this line:

if(method_exists($SP, $SP->$sanitized['function']()))

I perhaps incorrectly thought this would be how I could use my new variable ($santized) but it looks like I'm totally wrong. What would be the most efficient way to tackle this problem?

  • 写回答

1条回答 默认 最新

  • dongshi9719 2013-12-19 04:33
    关注

    You're using an object method, not a native function, as the array_map callback parameter. Try calling array_map like this:

    $sanitized = array_map(array($this, 'sanitize'), $_POST);
    

    See Callbacks for more info.

    method_exists just needs a string of the method name as its second parameter, try calling it like:

    method_exists($SP, $sanitized['function']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?