2017年2月22日 星期三

type juggling of PHP function default argument


在 php 版本 7.1.2 (Archlinux)
當 default argument 是 string ,
在指定 value 成 array 時會出現和預期不同的結果。

write2DBarcode([], 'QRCODE,H', 1,2,3,4);
Illegal string offset 'position'

tcpdf.php ( http://tcpdf.org/ )

public function write2DBarcode($code, $type, $x='', $y='', $w='', $h='', $style='', $align='', $distort=false) {
        if (!isset($style['position'])) {
            $style['position'] = ''; // error line

        }
}

workaround:
write2DBarcode([], 'QRCODE,H', 1,2 ,3 ,4 , []);