为什么 php 从不运行的类函数中生成回显?



我正在运行以下代码:

class column_field_length_validation extends Controller
{
private $input_column_type;
private $input_column_field_length;
private $input_csv_line_data;

public function __construct ($recieved_input_column_type, $recieved_input_column_field_length, $received_input_csv_line_data)
{
$this->input_column_type = $recieved_input_column_type;
$this->input_column_field_length = $recieved_input_column_field_length;
$this->input_csv_line_data = $received_input_csv_line_data;
}
public function run ()
{
$type_check = $this->input_column_type;
$csv_line_data = $this->input_csv_line_data;
$column_field_length = $this->input_column_field_length;
$type_check_function = [
'string_check' => $this->string_length_check($column_field_length, $csv_line_data),
];
foreach ($type_check_function as $key => $value) {
if ($type_check == $key) {
return $value;
}
}
}
public function string_length_check ($column_field_length, $csv_line_data)
{
$max_string_length = $column_field_length / 3;
$length_check = (strlen($csv_line_data) <= $max_string_length) ? 1 : 0;
echo 'string check preformed = ';
return [
'errors' => $length_check,
];
}
} 

我遇到的问题是,当调用此类时,$type_check不等于"string_check"时,"string_length_check"函数似乎没有运行。然而,回波线仍在运行。

上面的类/代码通过下面的类代码运行:

class check_csv_data_against_column_types extends Controller
{
public function run(Request $request)
{
$headings_to_check = (new columns_to_be_checked)->run($request);
$table_data = (new return_table_data)->run($request);
$table_name = $table_data['table_name'];
$column_types = $table_data['column_types'];
$column_lengths = $table_data['column_lengths'];
$csv_file = $_FILES['csvfile']['tmp_name'];
$handle = fopen($csv_file, "r");
$csv_data = fgetcsv($handle); 
$heading_count = count($headings_to_check['headings']);
$db = DB::table($table_name)->get();
while (! feof($handle)) {
$csv_line = fgetcsv($handle);
for ($i=0; $i < $heading_count; $i++) { 
$current_heading = $headings_to_check['headings'][$i];
$current_heading_position = $headings_to_check['positions'][$i];
$current_column_length = $column_lengths[$i];
$column_type_position = $column_types[$current_heading_position];
$line = $csv_line[$current_heading_position] ?? 'empty line break';
if ($line == 'empty line break') {
break;
}
$type_check = (new column_type_check_required($column_type_position))->run();
echo $type_check;
echo nl2br("rn");
print_r((new csv_line_data_column_type_validation($type_check, $line))->run());
echo nl2br("rn");
print_r((new column_field_length_validation($type_check, $current_column_length, $line))->run());
echo nl2br("rn");
}
}
fclose($handle);
}
}

结果或输出如下所示:

int_check
Array ( [errors] => 1 [csv_line_data] => 1 )
EXAMPLE --> string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => for sale )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 2 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => for rent )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 3 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => off market )
string check preformed = Array ( [errors] => 1 )
int_check
Array ( [errors] => 1 [csv_line_data] => 4 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
timestamp_check
Array ( [errors] => 1 [csv_line_data] => 2021-04-14 19:47:14 )
string check preformed =
string_check
Array ( [errors] => 1 [csv_line_data] => sold (subject to contract) )
string check preformed = Array ( [errors] => 1 )

这不是一个大问题,但知道我是否做错了什么或如何解决这个问题会很有帮助。

提前谢谢。

该代码正在运行,因为您将方法作为值调用,那么该数组索引的值是多少?方法的分辨率,因此方法运行。你要做的是关闭以防止这种情况发生。

所以,这个代码:

$type_check_function = [
'string_check' => $this->string_length_check($column_field_length, $csv_line_data),
];

应该是这样的:

$type_check_function = [
'string_check' => function () use ($column_field_length, $csv_line_data) {
return $this->string_length_check($column_field_length, $csv_line_data);
}
];

然后,你必须改变:

foreach ($type_check_function as $key => $value) {
if ($type_check == $key) {
return $value;
}
}

收件人:

foreach ($type_check_function as $key => $value) {
if ($type_check == $key) {
return $value();
}
}

因为$value将是:

function () use ($column_field_length, $csv_line_data) {
return $this->string_length_check($column_field_length, $csv_line_data);
}

因此,它将执行该函数,即$value(),并将返回$this->string_length_check($column_field_length, $csv_line_data)的结果。

也许我们必须调整一下,但我想我没有错过任何东西。

最新更新