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 求指导ADS低噪放设计
  • ¥15 CARSIM前车变道设置
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存