0

我正在更新一些基于发布数据调用函数的代码。原始类如下所示:

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"));
        }
    }
}

我正在尝试更新此类以在执行基于发布数据的函数之前清理数据。到目前为止,我已经得出以下结论:

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));
    }
}

这使我想到以下几点:

警告:array_map() 期望参数 1 是有效的回调,未找到函数 'sanitize' 或 C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress 中的函数名称无效。第 17 行的 php 致命错误:第 19 行的方法名称必须是 C:\DWASFiles\Sites\junglegym\VirtualDirectory0\site\wwwroot\wp-content\plugins\qcore\qcore_waitress.php 中的字符串

这是哪一行:

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

我可能错误地认为这就是我可以使用我的新变量 ( $santized) 的方式,但看起来我完全错了。解决这个问题的最有效方法是什么?

4

1 回答 1

1

您使用对象方法而不是本机函数作为array_map回调参数。尝试array_map这样调用:

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

有关详细信息,请参阅回调

method_exists只需要一个方法名称的字符串作为它的第二个参数,尝试调用它:

method_exists($SP, $sanitized['function']);
于 2013-12-19T04:33:07.067 回答