I want to manage a set of text transformations in an associative array.
The example works, but produces notices. It won't work when the rules are evaluated in a different file then the file where the array is defined. How can I get around this problem?
Code
<?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";
}
Output
❯❯❯ 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'
The php-version is PHP 5.3.27 and I have to stay compatible to 5.3 for now.