我正在努力更新一些基于帖子数据调用函数的代码。原始类看起来像这样:
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 \网站\ junglegym \ VirtualDirectory0 \网站\ wwwroot的\的wp-content \插件\ qcore \ qcore_waitress.php on line 17Fatal error:方法名称必须是字符串 C:\ DWASFiles \网站\ junglegym \ VirtualDirectory0 \网站\ wwwroot的\的wp-content \插件\ qcore \ qcore_waitress.php 第19行
这一行:
if(method_exists($SP, $SP->$sanitized['function']()))
我可能错误地认为这将是我如何使用我的新变量($santized
),但看起来我完全错了。什么是解决这个问题的最有效方法?
答案 0 :(得分:1)
您正在使用对象方法而不是本机函数作为array_map
回调参数。尝试像这样调用array_map
:
$sanitized = array_map(array($this, 'sanitize'), $_POST);
有关详细信息,请参阅Callbacks。
method_exists
只需要一个方法名字符串作为第二个参数,尝试调用它:
method_exists($SP, $sanitized['function']);