duanhua5523 2013-10-02 17:47
浏览 58
已采纳

嵌套函数,变量 - PHP

function partners($atts ) {
    extract(shortcode_atts(array(  
            'ids' => null,
            'extra_options' => 'something' <----------------- in wordpress I can read this value using local $extra_options 
    ), $atts));  
global $extra_options; <----------------- trying to change local var to global

function print_partners_scripts() {
    global $extra_options; <----------------- reading above variable
    echo '<script type="text/javascript">' . "
";
    echo 'jQuery(document).ready( function() {'. "
";
    echo '  $(".partners-slider").bxSlider({
        slideWidth: 924,
        auto: 0,
        autoStart: 0,
        moveSlides: 1,
        minSlides: 3,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        ' . $extra_options . ' <----------------- var is empty
     });' . "
";
    echo '});' . "
";
    echo '</script>' . "
";
} 
    add_action( 'wp_footer', 'print_partners_scripts' );

    $ids = explode( ',', $ids );
    $output = '<div class="ps-wrap"><div class="partners-slider">';
    foreach($ids as $id) {    
    $img_attr = wp_get_attachment_image_src( $id, 'full' );
    $output .= '<div class="pslide"><img src="' . $img_attr[0] . '" /></div>';    
}
    $output .= '</div></div>';

    return $output;  
}  

Hi, I'm trying to read var $extra_options inside print_partners_scripts(). The variable is set in the partners() function. I've tried to make it global and simply use it in certain place but I guess I'm doing something wrong ;)

Thanks in advance !

  • 写回答

2条回答 默认 最新

  • doulao1934 2013-10-02 18:09
    关注

    Firstly, PHP doesn't support nested functions in the way you are trying to use them.

    You can write this:

    function outer() { function inner() {} }
    outer();
    

    But all that happens is that when outer(); is executed, the inner() function is declared as a normal function. So the code is exactly the same as this:

    function outer() {}
    function inner() {}
    outer();
    

    Secondly, variables in PHP (unless prefixed, with a class or object name) are always scoped to the current function. The global keyword imports a reference to a global variable into the current function's scope; it cannot be used to export a variable which was already defined.

    It is generally best to only use the global keyword at the very beginning of a function, to import all the global variables needed by that function. Even better, do not use global variables, since they lead to "spaghetti code" which is hard to understand and debug.

    If you declare the variable global before running extract, this will work, but I would strongly advise against using either feature.

    function foo_with_too_much_magic()
    {
        // Import global variable. Very hard to track where this came from.
        global $some_var;
        // Let's assume this array comes from somewhere and isn't hard-coded
        $some_array = array('some_var' => 'some_value');
        // Export variables from an array. This is like telling PHP to write different code each time it runs, with different variable names.
        extract( $some_array );
    }
    foo_with_too_much_magic();
    var_dump($some_var);
    

    Here is a version of the above without the discouraged features:

    function foo_with_no_magic()
    {
        // Let's assume this array comes from somewhere and isn't hard-coded
        $some_array = array('some_var' => 'some_value');
        // You know which variable you want, so don't need the magic "export"
        // Note that you don't have to call it $some_var
        $some_var = $some_array['some_var'];
    
        // Now you have the variable, you can manipulate it, pass it to another function, or return it
        // In fact, you could also return $some_array['some_var'] directly, without the extra assignment
        return $some_var;
    }
    
    // This variable name no longer needs to be the same as what was used in the foo_with_no_magic() function
    $some_var = foo_with_no_magic();
    var_dump($some_var);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里