0

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.

tback
  • 11,138
  • 7
  • 47
  • 71

3 Answers3

2

Just quote the words, since you have not an array of functions, but of function names (as string) in your example.

$a = array(
" a " => "trim",
" b " => "noop",
" c " => "trimAndUpper",
);
Thomas Oster
  • 421
  • 2
  • 10
2

Unfortunately, functions are not first class in PHP and you cannot reference them by the symbol name. Instead, your code is trying to reference undefined constants (hence the notices). You can call a function name by string with call_user_func

" a " => "trim"
/* snip */
echo "key:'$key' value:'" . call_user_func($func, $key) . "'\n";
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

Your code:

echo "key:'$key' value:'{$func($key)}'\n";

The problem here is that the function call is inside the quoted string. Although you can reference variables like this, you can't call functions from inside a string.

Solution: Take the function call out of the string:

echo "key:'$key' value:'".$func($key)."'\n";

You also have a problem with the array definition:

" a " => trim,

The function names here (eg trim) can't be referenced simply by their name like this; you need to declare them as strings.

" a " => "trim",
Spudley
  • 166,037
  • 39
  • 233
  • 307
  • it works in the example. but it doesn't if the place were the function is called doesn't know the function referenced in the string. – tback Oct 09 '13 at 12:46
  • No, of course it doesn't. You can't call a function that PHP doesn't know about. A function must be loaded before you can call it. PHP provides an `function_exists()` function to check for this. There is also an autoload system in PHP, which allows you to make calls without explicitly including the relevant code first, but that only applies to classes, not functions. – Spudley Oct 09 '13 at 12:54