简体   繁体   English

PHP - 变量的函数名称

[英]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. 我正在使用Codeigniter。 It has a function in its Form helper to output a text field, which is 它在Form帮助器中有一个函数来输出一个文本字段,即

<?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'. 如果我回显这个变量,我确实得到了所需的值'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'. 我在这里真的很无能为力,因为只回显变量给出'form_input',但回显$ variable()只给'p'。

Where am I doing wrong? 我哪里做错了?

The actual problem here is that $instance is not an array, but a string. 这里的实际问题是$instance不是数组,而是字符串。 Judging from the error message, it's a string whose value starts with p . 从错误消息判断,它是一个以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. 语法$var[$key]不仅用于访问数组元素,还用于索引字符串,其中$var[0]将是$var等的第一个字符(实际上是字节)。如果$instance是一个字符串并且您编写$instance['taxon_field']然后PHP将尝试将'taxon_field'转换为整数以便索引到字符串。 This results in 0 as per the usual conversion rules , so the whole expression gets you the first letter of the string. 根据通常的转换规则 ,这会得到0 ,因此整个表达式会为您提供字符串的第一个字母。

Assuming that the string starts with p it's then pretty obvious why it tries to call a function with that name. 假设字符串以p开头,那么很明显为什么它会尝试调用具有该名称的函数。

使用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. 我的$ instance []数组实际上是foreach循环的结果(其中两个,确切地说)并且是更大的多维数组的一部分。 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 致命错误:在第69行的/opt/lampp/htdocs/bla.php中调用未定义的函数echo()

In other words, the newly defined function works fine, but the built-in 'echo' doesn't. 换句话说,新定义的函数工作正常,但内置的'echo'却没有。

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. 而在foreach循环中从多维数组创建函数会将数组转换为具有其最后一个成员值的字符串。

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? 在我给出的简化示例中,为什么我得到的信息是echo()函数未定义,而新函数工作正常?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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