Question

I'm using PHP create_function to make a lamba-style function whose contents are defined by a heredoc style string. Everything was fine until I moved the code to a Hostgator account. Something about the Hostgator environment is causing the function to completely fail.

It LOOKS like the heredoc is working fine, and the create_function seems to be executing. But when the function itself gets called, it dumps a ton of errors claiming every local variable is undefined! I'm baffled! Anyone seen this before? Here's the code:

$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);

Here is the error output:

Notice: Undefined variable: atts in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 129

Notice: Undefined variable: col_and_gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 129

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 129

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 131

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 132

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 133

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 134

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 135

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 136

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 139

Notice: Undefined variable: id in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 139

Notice: Undefined variable: id in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 139

Notice: Undefined variable: id in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 140

Notice: Undefined variable: style in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 140

Notice: Undefined variable: style in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 140

Notice: Undefined variable: style in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: returnval in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: id in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: style in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: col_and_gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: gutter in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 141

Notice: Undefined variable: class in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 142

Notice: Undefined variable: returnval in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 142

Notice: Undefined variable: content in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 143

Notice: Undefined variable: returnval in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 144

Notice: Undefined variable: returnval in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php on line 145

Parse error: syntax error, unexpected ')' in /home3/ade207/public_html/wp-content/themes/armstrongparent/lib/shortcodes.php(148) : runtime-created function on line 1

Était-ce utile?

La solution

@Passerby. Thanks, that tipped me off to the problem.

All the $variables were empty blank areas!! This is because heredoc does automatic variable substitution. So a person might be wondering how come it worked fine in other environments: GoDaddy and Linode VPS but not on Hostgator...

The reason it broke on Hostgator is because the Heredoc line was originally a Nowdoc when I first uploaded it. It threw an error so I changed it to Heredoc by removing the single quotes. I wasn't thinking at all and stupidly didn't realize this kicks in variable substitution so all the variables were replaced with null strings instead of staying as string literals for the function code.

Evidentally, Hostgator isn't running PHP 5.3 or later which is why Nowdoc doesn't work. And really that means I need to modify the script to be more backward compatible anyway. I might just have to escape all the variables as ugly as that will look.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top