当我这样做时:
{some code}
previouslyDeclaredFunction($variable);
{some code}
我可以让 previousDeclaredFunction() 正常工作。
但是当我把它放在一个新函数中时:
function newFunction($variable){
echo $variable; //see if var passes in properly
{some code}
previouslyDeclaredFunction($variable);
{some code}
}
..然后调用:
newFunction($variable);
..突然它停止工作即使我能够从 newFunction() 中 echo() $variable 就好了,这意味着 newFunction() 被正确调用并且 $variable 被正确传递。显然,除非我删除整个外部功能,否则里面的某些东西将无法工作。PreviouslyDeclaredFunction() 包含在 php 脚本中,它确实从 newFunction() 中调用,但以某种方式对待 $variable 不同,即使 echo() 证明它正在被传入并且与之前的值完全相同。
编辑(好的,这里是真正的代码):
$test_tag = "afro";
cacheBuilder($test_tag); //declaration of function
function cacheBuilder($test_tag) {
$images = array();
$tags = array();
$imagetype = 'Hair';
$per_page = 60;
$orderby_view = FALSE;
echo $test_tag; //this works so var is passed in fine
$tags2 = $test_tag;
$tags = explode(',', $test_tag);
if( count($tags) == 1 && strlen($tags[0]) == 0 ) $tags = array();
$tag_url = urlencode($tags2);
$cachename = dirname( __FILE__ ) . '/cache-fp/' . $imagetype . '-' . $per_page . '-' . $page . '-' . ($orderby_view ? 'by_view' : 'by_date') . $tag_url . '.json';
$detailurl = get_option('image_detail_url');
$detailurl .= (strstr($detailurl, '?') === FALSE ? '?' : '&');
$json = array();
$images = array();
$posts = get_pix($imagetype, array('per_page' => $per_page, 'page' => $page, 'tags' => $tags), $orderby_view);
foreach( $posts['attachments'] as $ii => $post ) {
$ta = array();
$meta = array();
$imagesrclight2 = array();
// BWP - Theater mode
$ta['detail_url'] = $detailurl . 'uid=' . $post->post_author . '&img_id=' . $post->ID . '&theater';
$meta = get_post_meta(get_the_ID(), 'image_tag', false);
$ta['image_tags'] = implode(' ', $meta);
$ta['attachment_image'] = wp_get_attachment_image($image->ID, 'thumbnail');
$imagesrclight2 = wp_get_attachment_image_src($image->ID, array(150, 150));
$ta['attachment_image_src'] = rawurlencode($imagesrclight2[0]);
$images[] = $ta;
}
file_put_contents($cachename, json_encode($images));
}
这有点复杂,它是 Wordpress,所以我希望这不会完全令人困惑。虽然没有错误,但看起来 get_option 和/或 get_pix 在外部函数内部失败了。生成的 json 中没有数据。当我摆脱外部函数时,我得到了 json ,其中填充了应有的数据。