一連のテキスト変換を連想配列で管理したいと考えています。
この例は機能しますが、通知が生成されます。配列が定義されているファイルとは別のファイルでルールが評価される場合は機能しません。どうすればこの問題を回避できますか?
コード
<?php
function noop($x){
return $x;
}
function trimAndUpper($x){
return strtoupper(trim($x));
}
$a = array(
" a " => trim,
" b " => noop,
" c " => trimAndUpper,
);
foreach($a as $key=>$func){
echo "key:'$key' value:'{$func($key)}'\n";
}
出力
❯❯❯ php ./experiment.php
PHP Notice: Use of undefined constant trim - assumed 'trim' in /tback/src/experiment.php on line 12
Notice: Use of undefined constant trim - assumed 'trim' in /tback/src/experiment.php on line 12
PHP Notice: Use of undefined constant noop - assumed 'noop' in /tback/src/experiment.php on line 13
Notice: Use of undefined constant noop - assumed 'noop' in /tback/src/experiment.php on line 13
PHP Notice: Use of undefined constant trimAndUpper - assumed 'trimAndUpper' in /tback/src/experiment.php on line 14
Notice: Use of undefined constant trimAndUpper - assumed 'trimAndUpper' in /tback/src/experiment.php on line 14
key:' a ' value:'a'
key:' b ' value:' b '
key:' c ' value:'C'
PHP のバージョンは PHP 5.3.27 で、今のところ 5.3 との互換性を維持する必要があります。