簡體   English   中英

在類中定義私有變量

[英]Defining a private variable in a class

我有以下課程

    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; 設置變量,但是我知道這不是正確的方法

任何建議正確執行此操作

不要使用全局。 這是您清除私有變量的方法;

class PieterGoosen_Widgets {

    private $sdwas; // Private variable

    public function myPublicFunction() {

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

    }

}

我建議將$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因為您將不需要它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM