Last active
September 5, 2024 01:34
-
-
Save thefuxia/3804204 to your computer and use it in GitHub Desktop.
Plugin Class Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php # -*- coding: utf-8 -*- | |
/** | |
* Plugin Name: Plugin Class Demo | |
* Description: How I am using the base class in plugins. | |
* Plugin URI: | |
* Version: 2012.09.29 | |
* Author: Fuxia Scholz | |
* License: GPL | |
* Text Domain: plugin_unique_name | |
* Domain Path: /languages | |
*/ | |
/* | |
* I prefer the empty-constructor-approach showed here. | |
* | |
* Advantages: | |
* - Unit tests can create new instances without activating any hooks | |
* automatically. No Singleton. | |
* | |
* - No global variable needed. | |
* | |
* - Whoever wants to work with the plugin instance can just call | |
* T5_Plugin_Class_Demo::get_instance(). | |
* | |
* - Easy to deactivate. | |
* | |
* - Still real OOP: no working methods are static. | |
* | |
* Disadvantage: | |
* - Maybe harder to read? | |
* | |
*/ | |
add_action( | |
'plugins_loaded', | |
array ( T5_Plugin_Class_Demo::get_instance(), 'plugin_setup' ) | |
); | |
class T5_Plugin_Class_Demo | |
{ | |
/** | |
* Plugin instance. | |
* | |
* @see get_instance() | |
* @type object | |
*/ | |
protected static $instance = NULL; | |
/** | |
* URL to this plugin's directory. | |
* | |
* @type string | |
*/ | |
public $plugin_url = ''; | |
/** | |
* Path to this plugin's directory. | |
* | |
* @type string | |
*/ | |
public $plugin_path = ''; | |
/** | |
* Access this plugin’s working instance | |
* | |
* @wp-hook plugins_loaded | |
* @since 2012.09.13 | |
* @return object of this class | |
*/ | |
public static function get_instance() | |
{ | |
NULL === self::$instance and self::$instance = new self; | |
return self::$instance; | |
} | |
/** | |
* Used for regular plugin work. | |
* | |
* @wp-hook plugins_loaded | |
* @since 2012.09.10 | |
* @return void | |
*/ | |
public function plugin_setup() | |
{ | |
$this->plugin_url = plugins_url( '/', __FILE__ ); | |
$this->plugin_path = plugin_dir_path( __FILE__ ); | |
$this->load_language( 'plugin_unique_name' ); | |
// more stuff: register actions and filters | |
} | |
/** | |
* Constructor. Intentionally left empty and public. | |
* | |
* @see plugin_setup() | |
* @since 2012.09.12 | |
*/ | |
public function __construct() {} | |
/** | |
* Loads translation file. | |
* | |
* Accessible to other classes to load different language files (admin and | |
* front-end for example). | |
* | |
* @wp-hook init | |
* @param string $domain | |
* @since 2012.09.11 | |
* @return void | |
*/ | |
public function load_language( $domain ) | |
{ | |
load_plugin_textdomain( | |
$domain, | |
FALSE, | |
$this->plugin_path . '/languages' | |
); | |
} | |
} |
Quick question about this, sorry if it seems obvious or is really basic – but is there a reason to even have an empty __construct()
for the Constructor, if plugin_setup()
here is setting things up and doing all the plugin actions?
I'm just starting to learn about OOP and see information about __construct()
methods in places like this StackOverlow answer that say it should be used for setting class properties – or is this true only if the properties as passed as arguments to the class, like in that answer I linked to above?
To clarify:
- Is
__construct()
needed for defining a class's properties? - If it's wholly not needed, then why leave an empty version of it in the class at all, instead of just leaving property definitions, hooks, actions, etc. in a custom init method like this demo's
plugin_setup()
method?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The following should be changed because
$this->plugin_path
already ends in a slash thanks toplugin_dir_path
always returning the path with the trailing slashChange from:
To: