0

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?

6
  • 1
    I created a sample PHP file with function form_field(){echo 'yay';} $instance=array('taxon_field'=>'form_field'); $instance['taxon_field'](); .. and it worked perfectly fine. Sounds like $instance is a string, not an array (in your case) Commented Sep 13, 2012 at 22:38
  • Pretty sure this was a php 5.3 addition. Commented Sep 13, 2012 at 22:39
  • Thanks for the feedback newfurniturey, at least I'm glad I'm not going crazy :) But I've been banging my head about this for an hour, and am still not able to figure out what am I doing wrong. Commented Sep 13, 2012 at 22:40
  • wes I'm running PHP Version 5.4.4 Commented Sep 13, 2012 at 22:41
  • To give some more clarification. If I put $name = $instance['taxon_field'] and then echo $name, I get the right $name value. But if I echo $name(), suddenly my $instance array's value is converted to the value of $instance['taxon_value'], which is the last array member of $instance. Commented Sep 14, 2012 at 11:00

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Doesn't the fact that he get the "Call to undefined function p()" error mean that it actually does work to call a function in that way?
I forgot to mention that I tried that too. Unfortunately, it's giving the same warning and the same error.
MrAzulay I'm not sure. If it is so, how (and why) does it change the function name from 'form_input' (or anything else that's stored in the given variable, for that matter) to only 'p'?
@ТомицаКораћ: Please see the updated answer for an explanation.
@Jon you're right. It does make sense to me now. But why (oh why?!) am I still not able to get what I want even if I store the array's value in a single variable? Especially because $var = 'form_input' does work as expected, but $var = $instance['taxon_field'] doesnt?
|
1

Use call_user_func()

call_user_func($instance['taxon_field']);

4 Comments

I am aware that your answer should work. But it's not working in my application for a reason I can't seem to find out, and that's confusing me.
I think we need to see the complete script
Also works for me: codepad.org/7CZMGUEG . codepad.org is running PHP5.2 , I had tested it local with PHP 5.4.4 , same result.
Okay, I think I have a clue about why this isn't working, but still don't know how to override it. I'll post more info in a new answer.
0

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?

6 Comments

You receive the error, because echo is no function(the manual calls it "language construct"). Try e.g. "var_dump", and you'll see, that it works with "real" built-in functions.
Now that we are working with an multidimensional array: are you sure that all items of $bigger_array are arrays? What returns var_dump($content['id']) (related to your post at the codeigniter-forum)
Oh wow, you're right about "echo". Stupid me. Thanks for clarifying that, it actually makes sense. var_dump($content['id'] gives this: pastebucket.com/2918
I also get the expected result with $content['id'] : codepad.org/n7RZR2X7 (also tested in PHP 5.4.4 again) . Did you also get this result when you run this on your server?
The thing is, I do get that result with an array which I define in the same view file. Unfortunately, as I said, it is a Codeigniter project, and I need to achieve the same with an array passed from a controller to the view. And if I try to do the same thing with two identical arrays (one passed from the controller, and the other defined in the view file), I get the mentioned errors for the former and the expected result for the latter. This is beginning to frustrate me.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.