PHP Lamba风格的函数代码抛出了大量未定义的变量错误



我正在使用PHP create_function创建一个lamba样式的函数,该函数的内容由heredoc样式的字符串定义。一切都很好,直到我把代码转移到Hostgator帐户。Hostgator环境的某些原因导致该功能完全失败。

看起来heredoc运行良好,create_function似乎正在执行。但当函数本身被调用时,它会转储大量错误,声称每个局部变量都是未定义的!我很困惑!有人见过这个吗?这是代码:

$func_code = <<<EOT
extract( shortcode_atts( array('style' => '','class' => '', 'id' => '', 'gutter' => 'm'), $atts ) );
$col_and_gutter = isset($gutter) ? 'needle-' . $gutter : 'needle-m';
switch ($gutter) {
    case 'ew': $gutter = 'col-extra-wide-gutter'; break;
    case 'w': $gutter = 'col-wide-gutter'; break;
    case 'm': $gutter = 'col-med-gutter'; break;
    case 'n': $gutter = 'col-narrow-gutter'; break;
    case 'none': $gutter = 'col-no-gutter'; break;
}
if ($id != '') $id = 'id="' . $id . '"';
if ($style != '') $style = 'style="' . $style . '"';
$returnval =  '<div ' . $id . ' ' . $style . ' class="col-responsive ' . $col_and_gutter . " " . $gutter . " " . $class . '">';
$returnval .= '<div class="g1"><div class="g2"><div class="g3">' . do_shortcode($content) . '</div></div></div>';
$returnval .= '</div>';
return $returnval;
EOT;            
$func_code = str_replace('needle', $shortcode, $func_code);
$func_handle = create_function('$atts,$content', $func_code); 
add_shortcode($shortcode, $func_handle);
add_shortcode($shortcode . '-n', $func_handle);
add_shortcode($shortcode . '-nn', $func_handle);

以下是错误输出:

注意:未定义的变量:atts in/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线129

注意:未定义的变量:中的col_and_digh/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线129

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线129

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线131

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php132线上

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线133

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线134

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线135

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线136

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线139

注意:未定义的变量:中的id/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线139

注意:未定义的变量:中的id/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线139

注意:未定义的变量:中的id/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线140

注意:未定义的变量:中的样式/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线140

注意:未定义的变量:中的样式/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线140

注意:未定义的变量:中的样式/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的returnval/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的id/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的样式/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的col_and_digh/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的檐沟/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线141

注意:未定义的变量:中的类/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线142

注意:未定义的变量:中的returnval/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线142

注意:未定义的变量:中的内容/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线143

注意:未定义的变量:中的returnval/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线144

注意:未定义的变量:中的returnval/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php在线145

分析错误:语法错误,中出现意外的")"/home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php(148):运行时在第1行上创建的函数

@路人。谢谢,这让我知道了这个问题。

所有$变量都是空的空白区域!!这是因为heredoc会自动替换变量。因此,有人可能想知道它为什么在其他环境中运行良好:GoDaddy和Linode VPS,但在Hostgator上却没有。。。

它在Hostgator上崩溃的原因是,当我第一次上传它时,Heredoc行最初是一个Nowdoc。它引发了一个错误,所以我通过删除单引号将其更改为Heredoc。我根本没有思考,愚蠢地没有意识到这会导致变量替换,所以所有变量都被替换为空字符串,而不是作为函数代码的字符串文本。

显然,Hostgator没有运行PHP 5.3或更高版本,这就是Nowdoc不起作用的原因。实际上,这意味着我需要修改脚本,使其更向后兼容。我可能只需要逃离所有看起来丑陋的变量。

最新更新