嵌套函数,变量 - PHP

时间:2013-10-02 17:47:07

标签: php wordpress function variables

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">' . "\n";
    echo 'jQuery(document).ready( function() {'. "\n";
    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
     });' . "\n";
    echo '});' . "\n";
    echo '</script>' . "\n";
} 
    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;  
}  

嗨,我正在尝试在print_partners_scripts()中读取var $ extra_options。变量在partners()函数中设置。我试图让它全球化,只是在某个地方使用它,但我想我做错了;)

提前致谢!

2 个答案:

答案 0 :(得分:1)

首先,PHP不支持您尝试使用嵌套函数的方式。

你可以这样写:

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

但所有这一切都是在执行outer();时,inner()函数被声明为普通函数。所以代码与此完全相同:

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

其次,PHP中的变量(除非带有前缀,带有类或对象名称)始终作用于当前函数。 global关键字将引用导入全局变量到当前函数的范围内;它不能用于导出已定义的变量。

通常最好只在函数的最开始使用global关键字,以导入该函数所需的所有全局变量。更好的是,不要使用全局变量,因为它们会导致难以理解和调试的“意大利面条代码”。

如果您在运行global之前声明变量extract ,这将有效,但我强烈建议不要使用任何一项功能。< / p>

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

以上版本没有令人气馁的功能:

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 :(得分:0)

下面是一个将代码放入类格式的示例,该格式与我的方向一致,为PHP学习更多的OOP实践(http://php.net/manual/en/language.oop5.php)可能很有用:

#1) Get the data you wish to pass into your function.
$data = "TEST";  
get_partners($data);

#2) Call your function.
function get_partners($atts) {
    //Extract using ($att) passed in from your call.
    //The shortcode_atts function should be accessible by the file containing this function.
    extract(shortcode_atts(array(
    'ids' => null,
    'extra_options' => 'something' //in wordpress I can read this value using local $extra_options
    ), $atts));
    //Create a new class element that will build your data for your and allow you to pass in your variable on the fly.
    $p = new partners();
    $p->extra_options= $atts; //Pass the variable here.
    $p->print_partners_scripts();
}

#3) Define Class here.
class partners {
    var $extra_options;

    public function print_partners_scripts()
    {
        $output = '<script type="text/javascript">' . "\n";
        $output .= 'jQuery(document).ready( function() {'. "\n";
        $output .= '  $(".partners-slider").bxSlider({
        slideWidth: 924,
        auto: 0,
        autoStart: 0,
        moveSlides: 1,
        minSlides: 3,
        maxSlides: 8,
        pager: false,
        controls: false,
        slideMargin: 5,
        ' . $this->extra_options . '
        });' . "\n";
        $output .= '});' . "\n";
        $output .= '</script>' . "\n";            
        $output .= $this->additional_data();
        echo $output;
    }

    protected function additional_data()
    {
        add_action( 'wp_footer', 'print_partners_scripts' );
        $ids; #Where is this defined?
        $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;
    }
}