简体   繁体   中英

PHP - Function name from variable

Okay, I've found a possible solution for this, but for some reason, I can't make it work in my application. Apparently, if I have a variable which contains a name function, I could use

<?php echo $variable(); ?>

to output the function with the same name.

I'm using Codeigniter. It has a function in its Form helper to output a text field, which is

<?php form_input(); ?>

I have a variable

<?php $instance['taxon_field'] = 'form_input'; ?>

If I echo out this variable, I do get the needed value, 'form_input'. However, as soon as I try to echo

$instance['taxon_field']()

I get a warning:

Message: Illegal string offset 'taxon_field'

and a fatal error:

Call to undefined function p()

I am really clueless here, because echoing only the variable gives 'form_input', but echoing $variable() only gives 'p'.

Where am I doing wrong?

The actual problem here is that $instance is not an array, but a string. Judging from the error message, it's a string whose value starts with p .

The syntax $var[$key] is used not only to access array elements but also to index into strings, where $var[0] would be the first character (actually, byte) of $var etc. If $instance is a string and you write $instance['taxon_field'] then PHP will try to convert 'taxon_field' to an integer in order to index into the string. This results in 0 as per the usual conversion rules , so the whole expression gets you the first letter of the string.

Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name.

使用call_user_func()

call_user_func($instance['taxon_field']);

The confusion created is actually my own fault because I failed to provide some aditional information which I thought was not important, but turned out to be crutial. My $instance[] array is actually a result of a foreach loop (two of them, to be precise) and is a part of a bigger multidimensional array. The actual code is more complicated, but I'll try to represent it right:

<?php
$bigger_array = array(
    0 => array(
        'field_one' => 'value_one',
        'field_two' => 'value_two',
        'field_three' => 'new_function'
    ),
    1 => array(
        'field_one' => 'new_value_one',
        'field_two' => 'new_value_two',
        'field_three' => 'echo'
    )
);

function new_function()
{
    echo 'New function called.';
}

foreach($bigger_array as $instance)
{
    $name = $instance['field_three'];
    $name('Hello World!');
}
?>

This will output the following:

New function called. Fatal error: Call to undefined function echo() in /opt/lampp/htdocs/bla.php on line 69

In other words, the newly defined function works fine, but the built-in 'echo' doesn't.

This is actually not my original problem, this is something that I've encountered while trying to debug the initial issue. And the original problem is that creating a function from a single-dimensional array works okay. whereas creating a function from a multi-dimensional array within a foreach loop transforms the array into a string with the value of its last member.

Now, I'm still not really able to fully answer my question, but I think information I'm giving could lead to a solution. In the simplified example that I gave here, why am I getting the message that echo() function is not defined, while the new function works fine?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM