如何将具有一个以类名命名的函数的父类升级到8.1,而不编辑调用该函数的每个子类


class tableBlock {
var $table_border = '0';
var $table_width = '100%';
var $table_cellspacing = '0';
var $table_cellpadding = '2';
var $table_parameters = '';
var $table_row_parameters = '';
var $table_data_parameters = '';
var $tableBox_string = '';
function tableBlock($contents){
$form_set = false;
...
if ($form_set == true) $tableBox_string .= '</form>' . "n";
return $tableBox_string;
}
}

目前,每次调用它时都会生成弃用警告,因为函数名和类名匹配当然只是暂时的。

该类由该网站中的许多其他类扩展,并在其他函数中调用为:$this->tableBlock($somearray);我已经尝试了许多添加CCD_ 2的方法,但没有找到一种维护函数"的方法;tableBlock";而且尝试的每一种方法都会产生致命的错误";调用不存在的函数";。有人成功地处理了这个问题吗?

为了减少代码更改,您可以将此方法设置为私有方法,并为此创建特殊的__call魔术方法。

这样,任何时候有人调用不存在的方法tableBlock都会命中__call代理方法。


class TableBlock {
private string $table_border = '0';
private string $table_width = '100%';
private string $table_cellspacing = '0';
private string $table_cellpadding = '2';
private string $table_parameters = '';
private string $table_row_parameters = '';
private string $table_data_parameters = '';
private string $tableBox_string = '';
public function __call(string $name, array $arguments): mixed
{
if ($name === 'tableBlock') {
return $this->makeTableBlock($arguments);
}
throw new Exception('Call to undefined method ' . $name);
}
private function makeTableBlock($contents): string
{
$form_set = false;
/* ... */
if ($form_set == true) {
$this->tableBox_string .= '</form>' . "n";
}
return $this->tableBox_string;
}
}

最新更新