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;
}
}
これでエラーは出力されなくなる。