php - Naming constants on a boilerplate class -


i'm creating boilerplate class use on every wordpress plugin create . (mods feel free edit if "boilerplate" not correct way call this). have constants use on plugins , on code, i'm looking correct way name them.

in code have :

//edit these define( 'wpb_prefix'                        , 'wsm'); define( 'wpb_slug'                          , 'wp-simple-monitor'); // need match plugin folder name define( 'wpb_plugin_name'                   , 'wp simple monitor'); define( 'wpb_version'                       , '1.0'); //dont edit define( wpb_prefix.'_abs_path'              , wp_plugin_dir . '/'. wpb_slug          ); define( wpb_prefix.'_rel_path'              , dirname( plugin_basename( __file__ ) )             ); define( wpb_prefix.'_plugin_url'            , wp_plugin_url . '/'. wpb_slug          );  class wp_plugin_base     {     // ...     _e('settings', wpb_prefix);     // ...     } 

so example on code use :

<?php _e('settings', wpb_prefix);?> 

so far ok, if end using 2 plugins in same project have check code , change "wpb_prefix" defined. there easy way of doing without going throw code?

thanks

you (and should!) define them class properties instead of global constants. can have base class sets defaults constants need access to, , extend class:

class wp_plugin_base {     protected $wpbprefix = 'wsm';     protected $wpbslug = 'wp-simple-monitor';     // etc. }  class wp_plugin_specific extends wp_plugin_base {     protected $wpbslug = 'wp-plugin-specific';      function somemethod() {         _e('settings', self::wpbprefix);         _e('slug', self::wpbslug);     } } 

the output of method in latter class original prefix, new slug. noted in comments, you'll need protected variables rather class constants in order able define things concatenated pieces, e.g.:

protected $abspath = wp_plugin_dir . '/' . $wpbslug; protected $relpath = dirname( plugin_basename( __file__ ) ); protected $pluginurl = wp_plugin_url . '/' . $wpbslug; 

that way, whenever use specific class need, benefits you're getting generic class have, don't have intersection issue.

this prevents trying figure out do if it's defined, , means whenever makes sense leave defaults in place, can: self::property retrieve base class property if haven't overridden it. should you're looking for, , won't clutter global namespace while you're @ it.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -