ミライハック
  • Home
  • Categories
  • About

>> Home / インターネット

FuelPHPでFormat::to_csv() should be compatible with that of Fuel\Core\Format::to_csv()が出た場合の対処法

∵ Takayoshi Saito ∴ 2016-12-31 ∞ 1'

FuelPHPのFormat::to_csv()をオーバーライドした場合のエラー対処

FuelPHPのFormat::to_csvはUTF-8対応で、Excel日本語版のShift-JISに対応していない。

QiitaのFuelPHPでCSVの出力を参考にto_csvをオーバーライドする記述をformat.phpに記述した。

<?php
// app/classes/format.php を作成
class Format extends Fuel\Core\Format {
    /**
     * CSV出力をSJIS-WINで返す
     * @access public
     * @param mixed $data
     * @return string csv(sjis-win)
     */
    public function to_csv($data = null){
        $csv = parent::to_csv($data);
        $csv = mb_convert_encoding($csv, 'SJIS-win', 'UTF-8');
        return $csv;
    }
}

そうしたら以下のエラーメッセージが出力された。

Fuel\Core\PhpErrorException [ Runtime Notice ]:
Declaration of Format::to_csv() should be compatible with that of Fuel\Core\Format::to_csv()
APPPATH/classes/format.php @ line 3

0
1<?php
2// app/classes/format.php を作成
3class Format extends Fuel\Core\Format {
4    /**
5     * CSV出力をSJIS-WINで返す
6     * @access public
7     * @param mixed $data
8     * @return string csv(sjis-win)

Backtrace

    COREPATH/bootstrap.php @ line 103
    APPPATH/classes/format.php @ line 3
    COREPATH/classes/autoloader.php @ line 365
    COREPATH/classes/autoloader.php @ line 236
    APPPATH/classes/controller/csv.php @ line 19
    COREPATH/classes/request.php @ line 454
    DOCROOT/index.php @ line 71
    DOCROOT/index.php @ line 96

対処法

to_csvの引数をcore側と合わせる。

    public function to_csv($delimiter = null, $enclose_numbers = null, array $headings = array()){
        $csv = parent::to_csv($data);
        $csv = mb_convert_encoding($csv, 'SJIS-win', 'UTF-8');
        return $csv;
    }
}

これでエラーは出力されなくなる。

Search

Categories
  • LInux
  • インターネット
  • インフラ
  • エッセイ
  • ゲーム
  • システム開発
  • セキュリティ
  • データサイエンス
  • 国際関係
  • 政治
  • 歴史
  • 社会学
  • 自己紹介
  • 行ってきた

Pages
  • 齊藤貴義
  • 職務経歴
  • スクレイピング・ハッキング・ラボ サポートページ
  • 『爆速開発を支えるClaude Code上級者テクニック』サポートページ

2026 © Takayoshi Saito | Twitter GitHub | Built on Zola