如何从父类的子类访问方法

时间:2019-05-13 13:08:07

标签: php

我有2个方法在同一个类中时可以正常工作,但是,我想将其中一个方法移到它自己的类中,以避免在一个类中占用过多的方法。但是,当我尝试执行此方法时,我将其中一个方法移到了自己的类中,因此无法正常工作。这些类用于将两个CSV文件合并在一起,然后将数据映射到相应的标头。

这是我要使用的类:

<?php

namespace CSVParser;

class Reader
{
    private $filePaths = [];
    protected $headers = [];
    protected $originalHeaders = [];
    protected $data = [];

    public function add(string $filePath): void
    {
        $this->filePaths[] = $filePath;
    }

    public function parseCSV(): void
    {
        foreach ($this->filePaths as $filePath) {
            $fp = fopen($filePath, 'r');

            $originalHeaders = fgetcsv($fp);
            $this->originalHeaders[] = $originalHeaders;
            fclose($fp);

            $this->headers = array_unique(array_merge($this->headers, $originalHeaders));
        }

        $i = 0;
        foreach ($this->filePaths as $i => $filePath) {
            $data = array_map('str_getcsv', file($filePath));
            unset($data[0]);

            $mapData = new Mapper();
            $this->data = array_merge($this->data, $mapData->mapData($i, $data));
        }
    }

还有另一类:

<?php

namespace CSVParser;

class Mapper extends Reader
{
    /**
     * Map data in column i to column j
     */
    protected function mapData($i, $data): array
    {
        $inputColumns = $this->originalHeaders[$i];
        $outputColumns = $this->headers;
        $columnIndex = array_flip($outputColumns);
        $mappedData = [];

        foreach ($data as $i => $row) {
            $workData = array_fill(0, count($this->headers), '');
            // Now take each input data and put it in the right place
            foreach ($row as $j => $value) {
                // Get name of source column
                $sourceColumn = $inputColumns[$j];
                // Where does that data go in the output
                $targetColumnIndex = $columnIndex[$sourceColumn];
                // Put the data there
                $workData[$targetColumnIndex] = $value;
            }
            // Store it in the mapped data
            $mappedData[] = $workData;
        }
        return $mappedData;
    }
}

这是我实例化类的index.php:

<?php

require_once '../vendor/autoload.php';

use CSVParser\Writer;

$writer = new Writer;

$writer->add('CSVFiles/sample1.csv');
$writer->add('CSVFiles/sample2.csv');
$writer->parseCSV();
$writer->writeCSV('CSVFiles/newsample.csv');

我认为mapData()和parseCSV()方法的工作顺序不正确,因此mapData()方法无法正确接收数据,并且出现了这些错误,但是我不知道如何进行这项工作。 这是我需要使用Mapper类的代码部分。

$i = 0;
        foreach ($this->filePaths as $i => $filePath) {
            $data = array_map('str_getcsv', file($filePath));
            unset($data[0]);

            $mapData = new Mapper();
            $this->data = array_merge($this->data, $mapData->mapData($i, $data));
        }

预期结果将是这样,如果我在同一个文件中同时使用了两种方法,这就是我得到的结果:

Name,Surname,Address,City,Gender
John,Doe,"Street 123",,
,Doe,,Riga,Male

但是,当我像上面的代码一样在各自的类中拆分方法时,会得到以下结果:

Name,Surname,Address,City,Gender
"Street 123"
Male

任何人都可以帮助我了解此处无法正常工作吗?

0 个答案:

没有答案