在类中定义私有变量

时间:2014-04-12 05:46:35

标签: php wordpress global-variables

我有以下课程

    class PieterGoosen_Widgets {

    public function __construct() {

        add_action( 'widgets_init', array( $this, 'sidebars_register' ) );
        add_action('add_meta_boxes', array( $this, 'add_cspp' ) );
        add_action('save_post', array( $this, 'save_cspp' ) );

    }

    public function sidebars_register() {

        $mws = array (
            'sidebar-2' => array (
                __( 'Main Sidebar', 'pietergoosen' ) => __( 'Main Sidebar for the website pages', 'pietergoosen' ),
            ),
            REST OF CODE NOT CONSTRUCTIVE TO QUESTION
            )
        );

        foreach ( $mws as $mi => $mw ) {
            foreach ($mw as $mwn => $mwd) {
                register_sidebar(
                    array (
                            'name'          =>  $mwn,
                            'id'            =>  $mi,
                            'description'   =>  $mwd,
                            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                            'after_widget'  => '</aside>',
                            'before_title'  => '<h1 class="widget-title">',
                            'after_title'   => '</h1>',
                    )
                );
            }
        }

        $options = pietergoosen_get_theme_options();
            global $sdwas;

        if(!empty($options['_custom_sidebar_per_page']))
            $sdwas = $options['_custom_sidebar_per_page'];

        if(!empty($sdwas) && sizeof($sdwas) > 0) {  
            foreach($sdwas as $sid => $sdwa) {
                $sid = self::sbslug($sdwa, 45);

                register_sidebar(
                    array (
                        'name'          => $sdwa,
                        'id'            => $sid,
                        'description'   => __( 'Page specific sidebars that can be chosen per page', 'pietergoosen' ),
                        'before_widget' => '<aside id="%1$s" class="widget %2$s">',
                        'after_widget'  => '</aside>',
                        'before_title'  => '<h1 class="widget-title">',
                        'after_title'   => '</h1>',
                    )
                );
            }
        }
    }

    public function sbslug($phrase, $maxLength) {
        $result = strtolower($phrase);

        $result = preg_replace("/[^a-z0-9\s-]/", "", $result);
        $result = trim(preg_replace("/[\s-]+/", " ", $result));
        $result = trim(substr($result, 0, $maxLength));
        $result = preg_replace("/\s/", "-", $result);

        return $result;
    }

    public function add_cspp() {

        add_meta_box( 
            'custom_sidebar_per_page', 
            __( 'Sidebar options', 'pietergoosen' ), 
            array( $this, 'cspp_link' ),
            'page', 
            'side', 
            'default'
        );
}

    public function cspp_link( $post ) {

        global $sdwas;

        $custom = get_post_custom($post->ID);

        if(!empty($custom['_custom_sidebar_per_page']))
            $val = $custom['_custom_sidebar_per_page'][0];
        else
            $val = "default";

        // The actual fields for data entry
        $output = '<p><label for="pietergoosen_new_field">'.__( 'Choose a sidebar to display', 'pietergoosen' ).'</label></p>';
        $output .= '<select name="custom_sidebar_per_page">';

        // Add a default option
        $output .= '<option';
        if($val == "default")
            $output .= ' selected="selected"';
        $output .= ' value="default">'.__( 'No Specified Sidebar', 'pietergoosen' ).'</option>';

        // Fill the select element with all registered sidebars
        if(!empty($sdwas))
            foreach($sdwas as $sid => $sdwa) {  
                $output .= '<option';  
                if($sdwa == $val)  
                    $output .= ' selected="selected"';  
                $output .= ' value="'.$sdwa.'">'.$sdwa.'</option>';  
            }  

        $output .= '</select>';  

        echo $output;  
    }  

    public function save_cspp($post_id){

        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_id;

        if ( !current_user_can( 'edit_page', $post_id ) )
            return;

        if(!empty( $_POST['custom_sidebar_per_page'] ))

        update_post_meta($post_id, '_custom_sidebar_per_page', $_POST['custom_sidebar_per_page']);
    }

}


$pgsidebar = new PieterGoosen_Widgets();

我需要将$sdwas设置为私有变量,以便在类public function中使用,但我不太清楚如何实现这一点。我总是使用global $sdwas;来设置变量,但我知道这不是真正的正确方法

有任何正确的建议吗

2 个答案:

答案 0 :(得分:1)

不要使用全球。这是你如何取消私有变量;

class PieterGoosen_Widgets {

    private $sdwas; // Private variable

    public function myPublicFunction() {

        // Print the private variable
        echo $this->sdwas;

    }

}

答案 1 :(得分:1)

我建议将$sdwas作为您班级的私人成员。

在顶部,您可以这样声明:

class PieterGoosen_Widgets {
    private $sdwas;
    //rest of code

然后,要在您的功能中访问此功能,只需使用$this->sdwas

$sdwas = $options['_custom_sidebar_per_page'];

变为:

$this->sdwas = $options['_custom_sidebar_per_page'];

请务必删除所有globals,因为您不需要它们。

相关问题