I'm developing a WordPress plugin. I'm new to PHP OOP, BTW adopted an advanced method for implementing things by following one of the advanced WordPress plugin. And now stuck somewhere. I can get back to procedural PHP, but things will be untouched if I get back from this far.
I understood what I did, but may be something I'm missing, and that's why I'm seeking your very intuitive look to my code, what am I doing wrong.
Main class with self instantiation
<?php
if ( ! class_exists( 'Xyz' ) ) :
final class Xyz {
/**
* Xyz version.
* @var string
*/
public $version = '1.0.0';
/**
* @var Xyz The single instance of the class
*/
protected static $_instance = null;
/**
* Main Xyz Instance.
*
* Ensures only one instance of Xyz is loaded or can be loaded.
*
* @static
* @see XYZ()
* @return Xyz - Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
public function __construct() {
$this->define_constants();
$this->xyz_includes();
$this->init();
}
/**
* Define constant if not yet set.
*
* @param string $name
* @param string|bool $value
*/
private function xyz_define( $name, $value ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* Define necessary constants
*/
private function define_constants() {
$this->xyz_define( 'XYZ_PLUGIN_FILE', __FILE__ );
$this->xyz_define( 'XYZ_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
}
/**
* Include additional files
*/
public function xyz_includes() {
/** Classes **/
include_once( 'includes/class-xyz-install.php' );
/** Core Functions **/
include_once( 'includes/xyz-core-functions.php' );
//...and so on...
}
public function init() {
register_activation_hook( __FILE__, array('XYZ_Install', 'install') );
add_action( 'init', array( $this, 'xyz_load_textdomain' ), 1 );
}
/**
* Make the plugin translation-ready.
*/
public function xyz_load_textdomain() {
load_plugin_textdomain(
'xyz',
false,
dirname( plugin_basename( __FILE__ ) ) .'/languages/'
);
}
}
endif;
/**
* Returns the main instance of Xyz to prevent the need to use globals.
* @return XYZ
*/
function XYZ() {
return Xyz::instance();
}
includes/class-xyz-install.php
<?php
class XYZ_Install {
public static function init() {
add_filter( 'plugin_action_links_'. XYZ_PLUGIN_BASENAME, array( __CLASS__, 'plugin_settings_link' ) );
}
public static function plugin_settings_link( $links ) {
//$links = existing links + made up link
return $links;
}
public function install() {
delete_option( 'xyz_version' );
add_option( 'xyz_version', XYZ()->version );
xyz_register_cpt_xyz();
flush_rewrite_rules( false );
}
}
XYZ_Install::init();
When I started developing the plugin the self instantiation was working fine, and I's getting all the values from its properties (var).
I enabled procedural coding for register_activation_hook() and other includes. But when I then implemented them using the final class, they stopped working.
- The includes are not working. If I put them a procedural function
something(), and simply call it somewheresomething();the includes are working fine. - I'm not sure whether my
register_activation_hook()inside the class is correct or not.
I'd love to learn PHP OOP from my mistakes. Any assistance would be greatly appreciated.
Edit 20180107
Fixed a typo NS_Install with XYZ_Install.