php - Defining a private variable in a class -


i have following 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 website pages', 'pietergoosen' ),             ),             rest of code not constructive question             )         );          foreach ( $mws $mi => $mw ) {             foreach ($mw $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 $sid => $sdwa) {                 $sid = self::sbslug($sdwa, 45);                  register_sidebar(                     array (                         'name'          => $sdwa,                         'id'            => $sid,                         'description'   => __( 'page specific sidebars can 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";          // actual fields data entry         $output = '<p><label for="pietergoosen_new_field">'.__( 'choose sidebar display', 'pietergoosen' ).'</label></p>';         $output .= '<select name="custom_sidebar_per_page">';          // add default option         $output .= '<option';         if($val == "default")             $output .= ' selected="selected"';         $output .= ' value="default">'.__( 'no specified sidebar', 'pietergoosen' ).'</option>';          // fill select element registered sidebars         if(!empty($sdwas))             foreach($sdwas $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(); 

i need set $sdwas private variable use across class' public functions, i'm not sure how achieve that. used global $sdwas; set variable, know isn't realy correct way

any suggestions

i recommend making $sdwas private member of class.

at top, can declare such:

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

then, access within functions, use $this->sdwas:

$sdwas = $options['_custom_sidebar_per_page']; 

becomes:

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

make sure remove globals not needing them.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -